@authend/sdk 0.1.0 → 0.1.1
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/README.md +177 -0
- package/bin/authend-gen.mjs +5 -4
- package/dist/client.d.ts +40 -10764
- package/dist/client.js +3 -0
- package/dist/types.d.ts +1 -1
- package/package.json +4 -3
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# @authend/sdk
|
|
2
|
+
|
|
3
|
+
Typed Authend client for external web, mobile, and server projects.
|
|
4
|
+
|
|
5
|
+
`@authend/sdk` is intentionally small:
|
|
6
|
+
|
|
7
|
+
- `auth`: Better Auth client access
|
|
8
|
+
- `data`: typed Authend resource access
|
|
9
|
+
|
|
10
|
+
It does not expose the admin control-plane surface.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @authend/sdk
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## How it works
|
|
19
|
+
|
|
20
|
+
The SDK uses two pieces:
|
|
21
|
+
|
|
22
|
+
1. The runtime client from `@authend/sdk`
|
|
23
|
+
2. Generated types from your deployed Authend backend
|
|
24
|
+
|
|
25
|
+
Your backend exposes a schema manifest at:
|
|
26
|
+
|
|
27
|
+
```txt
|
|
28
|
+
GET /api/system/sdk-schema
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The generator downloads that manifest and creates local TypeScript types for your tables and API resources.
|
|
32
|
+
|
|
33
|
+
## Generate types
|
|
34
|
+
|
|
35
|
+
Create `authend.config.json` in your app:
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"apiUrl": "http://localhost:7002",
|
|
40
|
+
"output": "./src/generated/authend.ts"
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Then run:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npx authend-gen generate
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
You can also add it to your app scripts:
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
{
|
|
54
|
+
"scripts": {
|
|
55
|
+
"authend:generate": "authend-gen generate"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
To create the config file automatically:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npx authend-gen init
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { createAuthendClient } from "@authend/sdk";
|
|
70
|
+
import { authendSchema, type AuthendSchema } from "./generated/authend";
|
|
71
|
+
|
|
72
|
+
const client = createAuthendClient<AuthendSchema>({
|
|
73
|
+
baseURL: "http://localhost:7002",
|
|
74
|
+
schema: authendSchema,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const posts = await client.data.post.list();
|
|
78
|
+
const post = await client.data.post.get("post_123");
|
|
79
|
+
|
|
80
|
+
await client.data.post.create({
|
|
81
|
+
title: "Hello",
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Auth
|
|
86
|
+
|
|
87
|
+
`client.auth` uses Better Auth client plugins.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
await client.auth.signIn.email({
|
|
91
|
+
email: "me@example.com",
|
|
92
|
+
password: "secret",
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
If you already have your own Better Auth client configured, pass it in:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { createAuthendClient } from "@authend/sdk";
|
|
100
|
+
import { auth } from "./auth";
|
|
101
|
+
import { authendSchema, type AuthendSchema } from "./generated/authend";
|
|
102
|
+
|
|
103
|
+
const client = createAuthendClient<AuthendSchema>({
|
|
104
|
+
baseURL: "http://localhost:7002",
|
|
105
|
+
schema: authendSchema,
|
|
106
|
+
authClient: auth,
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
This is the recommended pattern for mobile apps like Expo projects.
|
|
111
|
+
|
|
112
|
+
## Runtime shape
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
const client = createAuthendClient(...);
|
|
116
|
+
|
|
117
|
+
client.auth
|
|
118
|
+
client.data
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`client.data` supports:
|
|
122
|
+
|
|
123
|
+
- typed dot-notation access when you pass generated schema
|
|
124
|
+
- dynamic fallback access through `client.data.resource("table")`
|
|
125
|
+
|
|
126
|
+
Examples:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
await client.data.post.list();
|
|
130
|
+
await client.data.resource("post").list();
|
|
131
|
+
await client.data.meta("post");
|
|
132
|
+
await client.data.tables();
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## What gets generated
|
|
136
|
+
|
|
137
|
+
The generator emits:
|
|
138
|
+
|
|
139
|
+
- record types
|
|
140
|
+
- create input types
|
|
141
|
+
- update input types
|
|
142
|
+
- list param types
|
|
143
|
+
- runtime schema metadata used for typed dot-notation
|
|
144
|
+
|
|
145
|
+
Example generated output:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
export interface PostRecord {
|
|
149
|
+
id: string;
|
|
150
|
+
title: string;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export type PostCreateInput = {
|
|
154
|
+
title: string;
|
|
155
|
+
};
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## CLI
|
|
159
|
+
|
|
160
|
+
The package ships with `authend-gen`.
|
|
161
|
+
|
|
162
|
+
Available commands:
|
|
163
|
+
|
|
164
|
+
- `authend-gen init`
|
|
165
|
+
- `authend-gen generate`
|
|
166
|
+
|
|
167
|
+
Optional flags for `generate`:
|
|
168
|
+
|
|
169
|
+
- `--api-url`
|
|
170
|
+
- `--output`
|
|
171
|
+
- `--schema-output`
|
|
172
|
+
|
|
173
|
+
## Notes
|
|
174
|
+
|
|
175
|
+
- The SDK is for external consumers.
|
|
176
|
+
- The admin dashboard uses its own control-plane methods on top of the same backend.
|
|
177
|
+
- Full typing depends on the generated schema matching your deployed backend.
|
package/bin/authend-gen.mjs
CHANGED
|
@@ -129,7 +129,8 @@ function generateSource(manifest, apiUrl) {
|
|
|
129
129
|
"/* eslint-disable */",
|
|
130
130
|
`// Generated by authend-gen from ${apiUrl}/api/system/sdk-schema`,
|
|
131
131
|
`// Generated at ${manifest.generatedAt}`,
|
|
132
|
-
'import
|
|
132
|
+
'import { defineAuthendSchema } from "@authend/sdk";',
|
|
133
|
+
'import type { AuthendSchemaResource, ResourceListParams } from "@authend/sdk";',
|
|
133
134
|
"",
|
|
134
135
|
];
|
|
135
136
|
|
|
@@ -158,7 +159,7 @@ function generateSource(manifest, apiUrl) {
|
|
|
158
159
|
blocks.push(" };");
|
|
159
160
|
blocks.push("}");
|
|
160
161
|
blocks.push("");
|
|
161
|
-
blocks.push("export const authendSchema = {");
|
|
162
|
+
blocks.push("export const authendSchema = defineAuthendSchema<AuthendGeneratedSchema>({");
|
|
162
163
|
blocks.push(" resources: {");
|
|
163
164
|
for (const resource of manifest.resources) {
|
|
164
165
|
blocks.push(` ${resource.key}: {`);
|
|
@@ -173,7 +174,7 @@ function generateSource(manifest, apiUrl) {
|
|
|
173
174
|
blocks.push(" },");
|
|
174
175
|
}
|
|
175
176
|
blocks.push(" },");
|
|
176
|
-
blocks.push("}
|
|
177
|
+
blocks.push("});");
|
|
177
178
|
blocks.push("");
|
|
178
179
|
blocks.push("export type AuthendSchema = AuthendGeneratedSchema;");
|
|
179
180
|
blocks.push("");
|
|
@@ -195,7 +196,7 @@ async function commandInit(cwd) {
|
|
|
195
196
|
}
|
|
196
197
|
|
|
197
198
|
const config = {
|
|
198
|
-
apiUrl: "http://localhost:
|
|
199
|
+
apiUrl: "http://localhost:7002",
|
|
199
200
|
output: "./src/generated/authend.ts",
|
|
200
201
|
};
|
|
201
202
|
await writeFile(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf8");
|