@authend/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.js ADDED
@@ -0,0 +1,144 @@
1
+ import { apiKeyClient } from '@better-auth/api-key/client';
2
+ import { adminClient, jwtClient, magicLinkClient, organizationClient, twoFactorClient, usernameClient, } from 'better-auth/client/plugins';
3
+ import { createAuthClient } from 'better-auth/react';
4
+ const defaultAuthClientPlugins = [
5
+ usernameClient(),
6
+ jwtClient(),
7
+ organizationClient(),
8
+ twoFactorClient(),
9
+ apiKeyClient(),
10
+ magicLinkClient(),
11
+ adminClient(),
12
+ ];
13
+ export function createAuthendAuthClientPlugins(enabled) {
14
+ const enabledSet = new Set(enabled);
15
+ const plugins = [];
16
+ if (enabledSet.has('username')) {
17
+ plugins.push(usernameClient());
18
+ }
19
+ if (enabledSet.has('jwt')) {
20
+ plugins.push(jwtClient());
21
+ }
22
+ if (enabledSet.has('organization')) {
23
+ plugins.push(organizationClient());
24
+ }
25
+ if (enabledSet.has('twoFactor')) {
26
+ plugins.push(twoFactorClient());
27
+ }
28
+ if (enabledSet.has('apiKey')) {
29
+ plugins.push(apiKeyClient());
30
+ }
31
+ if (enabledSet.has('magicLink')) {
32
+ plugins.push(magicLinkClient());
33
+ }
34
+ if (enabledSet.has('admin')) {
35
+ plugins.push(adminClient());
36
+ }
37
+ return plugins;
38
+ }
39
+ export function createAuthendAuthClientPluginsFromManifest(manifests) {
40
+ return createAuthendAuthClientPlugins(manifests.filter((manifest) => manifest.installState.enabled).map((manifest) => manifest.id));
41
+ }
42
+ export function createAuthendClient(options) {
43
+ const auth = options.authClient ??
44
+ createAuthClient({
45
+ baseURL: options.baseURL,
46
+ fetch: options.fetch,
47
+ plugins: options.enabledPlugins === undefined ? defaultAuthClientPlugins : createAuthendAuthClientPlugins(options.enabledPlugins),
48
+ });
49
+ const request = async (path, init) => {
50
+ const response = await (options.fetch ?? fetch)(`${options.baseURL}${path}`, {
51
+ credentials: 'include',
52
+ headers: {
53
+ 'content-type': 'application/json',
54
+ ...(init?.headers ?? {}),
55
+ },
56
+ ...init,
57
+ });
58
+ if (!response.ok) {
59
+ const body = await response.text();
60
+ throw new Error(body || `Request failed: ${response.status}`);
61
+ }
62
+ if (response.status === 204) {
63
+ return undefined;
64
+ }
65
+ return (await response.json());
66
+ };
67
+ const resource = (table) => ({
68
+ list: (params) => {
69
+ const searchParams = new URLSearchParams();
70
+ const typedParams = params;
71
+ if (typedParams?.page) {
72
+ searchParams.set('page', String(typedParams.page));
73
+ }
74
+ if (typedParams?.pageSize) {
75
+ searchParams.set('pageSize', String(typedParams.pageSize));
76
+ }
77
+ if (typedParams?.sort) {
78
+ searchParams.set('sort', typedParams.sort);
79
+ }
80
+ if (typedParams?.order) {
81
+ searchParams.set('order', typedParams.order);
82
+ }
83
+ if (typedParams?.filterField) {
84
+ searchParams.set('filterField', typedParams.filterField);
85
+ }
86
+ if (typedParams?.filterValue) {
87
+ searchParams.set('filterValue', typedParams.filterValue);
88
+ }
89
+ if (typedParams?.include) {
90
+ searchParams.set('include', Array.isArray(typedParams.include) ? typedParams.include.join(',') : typedParams.include);
91
+ }
92
+ return request(`/api/data/${table}${searchParams.size > 0 ? `?${searchParams.toString()}` : ''}`);
93
+ },
94
+ get: (id) => request(`/api/data/${table}/${id}`),
95
+ create: (payload) => request(`/api/data/${table}`, {
96
+ method: 'POST',
97
+ body: JSON.stringify(payload),
98
+ }),
99
+ update: (id, payload) => request(`/api/data/${table}/${id}`, {
100
+ method: 'PATCH',
101
+ body: JSON.stringify(payload),
102
+ }),
103
+ remove: (id) => request(`/api/data/${table}/${id}`, {
104
+ method: 'DELETE',
105
+ }),
106
+ });
107
+ const schemaResources = (options.schema?.resources ?? {});
108
+ const dataBase = {
109
+ resource,
110
+ tables: () => request('/api/data'),
111
+ meta: (table) => request(`/api/data/meta/${table}`),
112
+ list: (table, searchParams) => resource(table).list(searchParams
113
+ ? {
114
+ page: searchParams.get('page') ? Number(searchParams.get('page')) : undefined,
115
+ pageSize: searchParams.get('pageSize') ? Number(searchParams.get('pageSize')) : undefined,
116
+ sort: searchParams.get('sort') ?? undefined,
117
+ order: searchParams.get('order') ?? undefined,
118
+ filterField: searchParams.get('filterField') ?? undefined,
119
+ filterValue: searchParams.get('filterValue') ?? undefined,
120
+ include: searchParams.get('include') ?? undefined,
121
+ }
122
+ : undefined),
123
+ get: (table, id) => resource(table).get(id),
124
+ create: (table, payload) => resource(table).create(payload),
125
+ update: (table, id, payload) => resource(table).update(id, payload),
126
+ remove: (table, id) => resource(table).remove(id),
127
+ };
128
+ const data = new Proxy(dataBase, {
129
+ get(target, prop, receiver) {
130
+ if (typeof prop !== 'string') {
131
+ return Reflect.get(target, prop, receiver);
132
+ }
133
+ if (prop in target) {
134
+ return Reflect.get(target, prop, receiver);
135
+ }
136
+ const routeSegment = schemaResources[prop]?.routeSegment ?? prop;
137
+ return resource(routeSegment);
138
+ },
139
+ });
140
+ return {
141
+ auth,
142
+ data,
143
+ };
144
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./client";
2
+ export * from "./types";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./client";
2
+ export * from "./types";
@@ -0,0 +1,39 @@
1
+ export type PluginId = 'username' | 'jwt' | 'organization' | 'twoFactor' | 'apiKey' | 'magicLink' | 'socialAuth' | 'admin';
2
+ export type DataRecord = Record<string, unknown>;
3
+ export type TableApiOperations = {
4
+ list: boolean;
5
+ get: boolean;
6
+ create: boolean;
7
+ update: boolean;
8
+ delete: boolean;
9
+ };
10
+ export type FieldBlueprint = {
11
+ name: string;
12
+ type: 'text' | 'varchar' | 'integer' | 'bigint' | 'boolean' | 'timestamp' | 'date' | 'jsonb' | 'uuid' | 'numeric' | 'enum';
13
+ nullable?: boolean;
14
+ default?: string | null;
15
+ unique?: boolean;
16
+ indexed?: boolean;
17
+ size?: number | null;
18
+ enumValues?: string[] | null;
19
+ references?: {
20
+ table: string;
21
+ column: string;
22
+ onDelete?: 'no action' | 'restrict' | 'cascade' | 'set null';
23
+ onUpdate?: 'no action' | 'restrict' | 'cascade' | 'set null';
24
+ } | null;
25
+ };
26
+ export type TableDescriptor = {
27
+ table: string;
28
+ primaryKey: string;
29
+ fields: FieldBlueprint[];
30
+ source: 'builtin' | 'generated' | 'plugin';
31
+ mutableSchema: boolean;
32
+ ownerPluginId?: PluginId | null;
33
+ };
34
+ export type PluginManifest = {
35
+ id: PluginId;
36
+ installState: {
37
+ enabled: boolean;
38
+ };
39
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@authend/sdk",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": {
9
+ "authend-gen": "./bin/authend-gen.mjs"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "bin"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc -p tsconfig.build.json",
23
+ "prepack": "npm run build"
24
+ },
25
+ "dependencies": {
26
+ "@better-auth/api-key": "^1.3.7",
27
+ "better-auth": "^1.3.7"
28
+ }
29
+ }