Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pocketbase mobile client and types definitions enhancements" #116

Merged
merged 10 commits into from
Mar 28, 2024
21 changes: 11 additions & 10 deletions docs/statements/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,17 @@ neither the open/connect part nor the parameters part are present.
| | token,server_data | get claims from token |

## pocketbase plugin
| open/connect | params | phrase |
| ------------ | ----------------------------------- | ---------------- |
| | pb_address | create pb_client |
| | my_credentials | login |
| | list_parameters | ask records |
| | show_parameters | ask record |
| | create_parameters,record_parameters | create record |
| | update_parameters,record_parameters | update record |
| | delete_parameters | delete record |
| | url,send_parameters | send request |
| open/connect | params | phrase |
| ------------ | ----------------------------------- | ---------------------------------- |
| | pb_address | connect to pb_address |
| | pb_address | connect capacitor app to pb_client |
| | my_credentials | login |
| | list_parameters | get some records |
| | show_parameters | get one record |
| | create_parameters,record_parameters | create record |
| | update_parameters,record_parameters | update record |
| | delete_parameters | delete record |
| | url,send_parameters | send request |

## qrcode plugin
| open/connect | params | phrase |
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@
"docs": "pnpm run docs:api && pnpm run docs:build"
},
"devDependencies": {
"@lerna-lite/publish": "^2.7.2",
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"ava": "^5.3.1",
"c8": "^8.0.1",
"esbuild": "^0.19.9",
"eslint": "^8.55.0",
"@lerna-lite/publish": "^3.3.1",
"@types/node": "^20.11.30",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"ava": "^6.1.2",
"c8": "^9.1.0",
"esbuild": "^0.20.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"nock": "14.0.0-beta.5",
"prettier": "^3.1.1",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"typedoc": "^0.25.4",
"typescript": "5.2.2",
"typedoc": "^0.25.12",
"typescript": "5.4.3",
"vitepress": "^1.0.1"
}
}
1 change: 1 addition & 0 deletions pkg/pocketbase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@slangroom/pocketbase",
"version": "1.27.6",
"dependencies": {
"@capacitor/preferences": "^5.0.7",
"@slangroom/core": "workspace:*",
"@slangroom/shared": "workspace:*",
"dotenv": "^16.3.1",
Expand Down
56 changes: 39 additions & 17 deletions pkg/pocketbase/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import PocketBase from 'pocketbase';
import PocketBase, { AsyncAuthStore } from 'pocketbase';
import type { FullListOptions, ListResult, RecordModel, RecordOptions } from 'pocketbase';
import { Plugin } from '@slangroom/core';
import { z } from 'zod';
import { Preferences } from '@capacitor/preferences';


let pb: PocketBase;
const p = new Plugin();
Expand All @@ -22,9 +24,9 @@ const credentialsSchema = z.object({
export type Credentials = z.infer<typeof credentialsSchema>;

const baseRecordParametersSchema = z.object({
expand: z.string().nullable(),
requestKey: z.string().nullable(),
fields: z.string().nullable(),
expand: z.string().nullish(),
requestKey: z.string().nullish(),
fields: z.string().nullish(),
});
export type RecordBaseParameters = z.infer<typeof baseRecordParametersSchema>;

Expand All @@ -39,15 +41,15 @@ const paginationSchema = z.object({

const listParametersBaseSchema = z
.object({
sort: z.string().nullable(),
sort: z.string().nullish(),
})
.merge(baseFetchRecordParametersSchema);

const listParametersSchema = z.discriminatedUnion('type', [
listParametersBaseSchema.extend({ type: z.literal('all'), filter: z.string().nullable() }),
listParametersBaseSchema.extend({ type: z.literal('all'), filter: z.string().nullish() }),
listParametersBaseSchema.extend({
type: z.literal('list'),
filter: z.string().nullable(),
filter: z.string().nullish(),
pagination: paginationSchema,
}),
listParametersBaseSchema.extend({ type: z.literal('first'), filter: z.string() }),
Expand Down Expand Up @@ -89,17 +91,17 @@ const isPbRunning = async () => {
const createRecordOptions = (p: RecordBaseParameters) => {
const { expand, fields, requestKey } = p;
const options: RecordOptions = {};
if (expand !== null) options.expand = expand;
if (fields !== null) options.fields = fields;
if (requestKey !== null) options.requestKey = requestKey;
if (expand && expand!==null) options.expand = expand;
if (fields && fields!==null) options.fields = fields;
if (requestKey && requestKey!==null) options.requestKey = requestKey;
return options;
};

/**
* @internal
*/
export const setupClient = p.new(['pb_address'], 'create pb_client', async (ctx) => {
const address = ctx.fetch('pb_address');
export const setupClient = p.new('connect', 'start pb client', async (ctx) => {
const address = ctx.fetchConnect()[0];
if (typeof address !== 'string') return ctx.fail('Invalid address');
try {
pb = new PocketBase(address);
Expand All @@ -110,6 +112,26 @@ export const setupClient = p.new(['pb_address'], 'create pb_client', async (ctx)
}
});

export const setupCapacitorClient = p.new('connect', 'start capacitor pb client', async (ctx) => {
const address = ctx.fetchConnect()[0];
const PB_AUTH_KEY:string = 'pb_auth'

const store = new AsyncAuthStore({
save: async (serialized) => Preferences.set({
key:PB_AUTH_KEY, value:serialized,
}),
initial: Preferences.get({ key:PB_AUTH_KEY }),
});
if (typeof address !== 'string') return ctx.fail('Invalid address');
try {
pb = new PocketBase(address, store);
if (!(await isPbRunning())) return ctx.fail('Client is not running');
return ctx.pass('pb client successfully created');
} catch (e) {
throw new Error(e)
}
});

/**
* @internal
*/
Expand All @@ -133,7 +155,7 @@ export const authWithPassword = p.new(['my_credentials'], 'login', async (ctx) =
/**
* @internal
*/
export const getList = p.new(['list_parameters'], 'ask records', async (ctx) => {
export const getList = p.new(['list_parameters'], 'get some records', async (ctx) => {
const params = ctx.fetch('list_parameters') as ListParameters;
const validation = listParametersSchema.safeParse(params);
if (!validation.success) return ctx.fail(validation.error);
Expand Down Expand Up @@ -162,7 +184,7 @@ export const getList = p.new(['list_parameters'], 'ask records', async (ctx) =>
/**
* @internal
*/
export const showRecord = p.new(['show_parameters'], 'ask record', async (ctx) => {
export const showRecord = p.new(['show_parameters'], 'get one record', async (ctx) => {
const p = ctx.fetch('show_parameters') as ShowRecordParameters;
const validation = showParametersSchema.safeParse(p);
if (!validation.success) return ctx.fail(validation.error);
Expand Down Expand Up @@ -248,10 +270,10 @@ export const deleteRecord = p.new(['delete_parameters'], 'delete record', async

const sendParametersSchema = z.object({
fetch: z.any(),
headers: z.record(z.unknown()).optional(),
headers: z.record(z.unknown()).nullish(),
body: z.any(),
query: z.record(z.unknown()).optional(),
requestKey: z.string().optional(),
query: z.record(z.unknown()).nullish(),
requestKey: z.string().nullish(),
});
export type SendParameters = z.infer<typeof sendParametersSchema>;

Expand Down
Loading
Loading