@cfast/core 0.1.4 → 0.3.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/chunk-N7V3D574.js +44 -0
- package/dist/index.d.ts +17 -6
- package/dist/index.js +6 -11
- package/dist/presets.d.ts +64 -0
- package/dist/presets.js +6 -0
- package/llms.txt +44 -0
- package/package.json +9 -5
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/define-plugin.ts
|
|
2
|
+
function definePlugin(config) {
|
|
3
|
+
if (config === void 0) {
|
|
4
|
+
return (innerConfig) => innerConfig;
|
|
5
|
+
}
|
|
6
|
+
return config;
|
|
7
|
+
}
|
|
8
|
+
function definePluginFor() {
|
|
9
|
+
return (config) => config;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/presets.ts
|
|
13
|
+
function createDefaultPlugins(config) {
|
|
14
|
+
const authPlugin = definePlugin({
|
|
15
|
+
name: "auth",
|
|
16
|
+
async setup(ctx) {
|
|
17
|
+
const auth = config.initAuth(ctx.env);
|
|
18
|
+
const authCtx = await auth.createContext(ctx.request);
|
|
19
|
+
return {
|
|
20
|
+
user: authCtx.user,
|
|
21
|
+
grants: authCtx.grants,
|
|
22
|
+
instance: auth
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const dbPlugin = definePlugin({
|
|
27
|
+
name: "db",
|
|
28
|
+
requires: [authPlugin],
|
|
29
|
+
setup(ctx) {
|
|
30
|
+
const client = config.createDb(
|
|
31
|
+
ctx.auth.grants,
|
|
32
|
+
ctx.auth.user ? { id: ctx.auth.user.id } : null
|
|
33
|
+
);
|
|
34
|
+
return { client };
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return { authPlugin, dbPlugin };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export {
|
|
41
|
+
definePlugin,
|
|
42
|
+
definePluginFor,
|
|
43
|
+
createDefaultPlugins
|
|
44
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Permissions } from '@cfast/permissions';
|
|
|
3
3
|
import { C as CreateAppConfig, A as App, a as CfastPlugin, P as PluginSetupContext, R as RequiresFromPlugins } from './types-CQyKq4D1.js';
|
|
4
4
|
export { b as AppContext, c as PluginContext, d as PluginProvides, e as RouteArgs } from './types-CQyKq4D1.js';
|
|
5
5
|
import { ComponentType, ReactNode } from 'react';
|
|
6
|
+
export { DefaultAuthProvides, DefaultDbProvides, DefaultPluginsConfig, createDefaultPlugins } from './presets.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Creates a cfast application instance that wires env, permissions, and plugins into a typed per-request context.
|
|
@@ -88,25 +89,35 @@ declare function createApp<TSchema extends Schema, TPermissions extends Permissi
|
|
|
88
89
|
* return { client: createDb({}) };
|
|
89
90
|
* },
|
|
90
91
|
* });
|
|
92
|
+
*
|
|
93
|
+
* // Env-typed plugin — pass the env type as the single explicit generic
|
|
94
|
+
* // to type ctx.env precisely (no casts needed):
|
|
95
|
+
* const storagePlugin = definePlugin<Cloudflare.Env>({
|
|
96
|
+
* name: 'storage',
|
|
97
|
+
* setup(ctx) {
|
|
98
|
+
* ctx.env.BUCKET; // typed as R2Bucket, not unknown
|
|
99
|
+
* return { upload: (file: File) => ctx.env.BUCKET.put(file.name, file) };
|
|
100
|
+
* },
|
|
101
|
+
* });
|
|
91
102
|
* ```
|
|
92
103
|
*/
|
|
93
|
-
declare function definePlugin<TName extends string, TProvides, const TRequires extends readonly CfastPlugin<string, unknown, any, unknown, any>[] = [], TClient = unknown>(config: {
|
|
104
|
+
declare function definePlugin<TEnv = Record<string, unknown>, TName extends string = string, TProvides = unknown, const TRequires extends readonly CfastPlugin<string, unknown, any, unknown, any>[] = [], TClient = unknown>(config: {
|
|
94
105
|
name: TName;
|
|
95
106
|
requires?: TRequires;
|
|
96
|
-
setup: (ctx: PluginSetupContext<RequiresFromPlugins<TRequires
|
|
107
|
+
setup: (ctx: PluginSetupContext<RequiresFromPlugins<TRequires>, TEnv>) => TProvides | Promise<TProvides>;
|
|
97
108
|
Provider?: ComponentType<{
|
|
98
109
|
children: ReactNode;
|
|
99
110
|
}>;
|
|
100
111
|
client?: TClient;
|
|
101
|
-
}): CfastPlugin<TName, Awaited<TProvides>, RequiresFromPlugins<TRequires>, TClient>;
|
|
102
|
-
declare function definePlugin<TRequires
|
|
112
|
+
}): CfastPlugin<TName, Awaited<TProvides>, RequiresFromPlugins<TRequires>, TClient, TEnv>;
|
|
113
|
+
declare function definePlugin<TRequires, TEnv = Record<string, unknown>>(): <TName extends string, TProvides, TClient = unknown>(config: {
|
|
103
114
|
name: TName;
|
|
104
|
-
setup: (ctx: PluginSetupContext<TRequires>) => TProvides | Promise<TProvides>;
|
|
115
|
+
setup: (ctx: PluginSetupContext<TRequires, TEnv>) => TProvides | Promise<TProvides>;
|
|
105
116
|
Provider?: ComponentType<{
|
|
106
117
|
children: ReactNode;
|
|
107
118
|
}>;
|
|
108
119
|
client?: TClient;
|
|
109
|
-
}) => CfastPlugin<TName, Awaited<TProvides>, TRequires, TClient>;
|
|
120
|
+
}) => CfastPlugin<TName, Awaited<TProvides>, TRequires, TClient, TEnv>;
|
|
110
121
|
/**
|
|
111
122
|
* Returns an env-typed `definePlugin` factory.
|
|
112
123
|
*
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDefaultPlugins,
|
|
3
|
+
definePlugin,
|
|
4
|
+
definePluginFor
|
|
5
|
+
} from "./chunk-N7V3D574.js";
|
|
1
6
|
import {
|
|
2
7
|
createCoreProvider
|
|
3
8
|
} from "./chunk-DFUBJVKG.js";
|
|
@@ -106,21 +111,11 @@ function buildApp(envInstance, permissions, plugins) {
|
|
|
106
111
|
};
|
|
107
112
|
return app;
|
|
108
113
|
}
|
|
109
|
-
|
|
110
|
-
// src/define-plugin.ts
|
|
111
|
-
function definePlugin(config) {
|
|
112
|
-
if (config === void 0) {
|
|
113
|
-
return (innerConfig) => innerConfig;
|
|
114
|
-
}
|
|
115
|
-
return config;
|
|
116
|
-
}
|
|
117
|
-
function definePluginFor() {
|
|
118
|
-
return (config) => config;
|
|
119
|
-
}
|
|
120
114
|
export {
|
|
121
115
|
CfastConfigError,
|
|
122
116
|
CfastPluginError,
|
|
123
117
|
createApp,
|
|
118
|
+
createDefaultPlugins,
|
|
124
119
|
definePlugin,
|
|
125
120
|
definePluginFor
|
|
126
121
|
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { a as CfastPlugin } from './types-CQyKq4D1.js';
|
|
2
|
+
import { Grant } from '@cfast/permissions';
|
|
3
|
+
import '@cfast/env';
|
|
4
|
+
import 'react';
|
|
5
|
+
|
|
6
|
+
type AuthInstanceLike = {
|
|
7
|
+
createContext: (request: Request) => Promise<{
|
|
8
|
+
user: {
|
|
9
|
+
id: string;
|
|
10
|
+
email: string;
|
|
11
|
+
name: string;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
} | null;
|
|
14
|
+
grants: Grant[];
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
type DbFactory = (grants: Grant[], user: {
|
|
18
|
+
id: string;
|
|
19
|
+
} | null) => unknown;
|
|
20
|
+
type DefaultPluginsConfig = {
|
|
21
|
+
initAuth: (env: Record<string, unknown>) => AuthInstanceLike;
|
|
22
|
+
createDb: DbFactory;
|
|
23
|
+
};
|
|
24
|
+
type DefaultAuthProvides = {
|
|
25
|
+
user: {
|
|
26
|
+
id: string;
|
|
27
|
+
email: string;
|
|
28
|
+
name: string;
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
} | null;
|
|
31
|
+
grants: Grant[];
|
|
32
|
+
instance: AuthInstanceLike;
|
|
33
|
+
};
|
|
34
|
+
type DefaultDbProvides = {
|
|
35
|
+
client: unknown;
|
|
36
|
+
};
|
|
37
|
+
declare function createDefaultPlugins(config: DefaultPluginsConfig): {
|
|
38
|
+
authPlugin: CfastPlugin<"auth", {
|
|
39
|
+
user: {
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
id: string;
|
|
42
|
+
email: string;
|
|
43
|
+
name: string;
|
|
44
|
+
} | null;
|
|
45
|
+
grants: Grant[];
|
|
46
|
+
instance: AuthInstanceLike;
|
|
47
|
+
}, unknown, unknown, Record<string, unknown>>;
|
|
48
|
+
dbPlugin: CfastPlugin<"db", {
|
|
49
|
+
client: unknown;
|
|
50
|
+
}, {
|
|
51
|
+
auth: {
|
|
52
|
+
user: {
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
id: string;
|
|
55
|
+
email: string;
|
|
56
|
+
name: string;
|
|
57
|
+
} | null;
|
|
58
|
+
grants: Grant[];
|
|
59
|
+
instance: AuthInstanceLike;
|
|
60
|
+
};
|
|
61
|
+
}, unknown, Record<string, unknown>>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { type DefaultAuthProvides, type DefaultDbProvides, type DefaultPluginsConfig, createDefaultPlugins };
|
package/dist/presets.js
ADDED
package/llms.txt
CHANGED
|
@@ -138,6 +138,50 @@ with it slot into existing `app.use()` calls without further changes. If you
|
|
|
138
138
|
prefer to type a single plugin in-place without a factory, import the
|
|
139
139
|
`PluginContext<Env, TRequires>` helper and annotate the parameter directly.
|
|
140
140
|
|
|
141
|
+
### Presets (`@cfast/core/presets`)
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { createDefaultPlugins } from "@cfast/core/presets";
|
|
145
|
+
|
|
146
|
+
function createDefaultPlugins(config: {
|
|
147
|
+
initAuth: (env: Record<string, unknown>) => AuthInstanceLike;
|
|
148
|
+
createDb: (grants: Grant[], user: { id: string } | null) => Db;
|
|
149
|
+
}): { authPlugin, dbPlugin }
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Creates the standard auth + db plugin pair that every cfast app uses. Replaces ~50 lines of identical plugin definitions across `cfast.server.ts` in every demo app.
|
|
153
|
+
|
|
154
|
+
The auth plugin calls `initAuth(ctx.env)` per-request and returns `{ user, grants, instance }` under `ctx.auth`. The db plugin depends on the auth plugin and calls `createDb(grants, user)` to return `{ client }` under `ctx.db`.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { createApp } from "@cfast/core";
|
|
158
|
+
import { createDefaultPlugins } from "@cfast/core/presets";
|
|
159
|
+
import { envSchema } from "./env";
|
|
160
|
+
import { permissions } from "./permissions";
|
|
161
|
+
import { initAuth } from "./auth.setup.server";
|
|
162
|
+
import { appDb } from "./db/client";
|
|
163
|
+
|
|
164
|
+
const { authPlugin, dbPlugin } = createDefaultPlugins({
|
|
165
|
+
initAuth: (env) => initAuth({
|
|
166
|
+
d1: env.DB as D1Database,
|
|
167
|
+
appUrl: env.APP_URL as string,
|
|
168
|
+
}),
|
|
169
|
+
createDb: (grants, user) => appDb(grants, user),
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
export const app = createApp({ env: envSchema, permissions })
|
|
173
|
+
.use(authPlugin)
|
|
174
|
+
.use(dbPlugin);
|
|
175
|
+
// Add app-specific plugins after the defaults:
|
|
176
|
+
// .use(storagePlugin)
|
|
177
|
+
// .use(emailPlugin);
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Also re-exported from `@cfast/core` main entry for convenience:
|
|
181
|
+
```typescript
|
|
182
|
+
import { createDefaultPlugins } from "@cfast/core";
|
|
183
|
+
```
|
|
184
|
+
|
|
141
185
|
### Client (`@cfast/core/client`)
|
|
142
186
|
|
|
143
187
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfast/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "App composition layer with plugin system for @cfast/* packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cfast",
|
|
@@ -26,6 +26,10 @@
|
|
|
26
26
|
"./client": {
|
|
27
27
|
"import": "./dist/client/index.js",
|
|
28
28
|
"types": "./dist/client/index.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./presets": {
|
|
31
|
+
"import": "./dist/presets.js",
|
|
32
|
+
"types": "./dist/presets.d.ts"
|
|
29
33
|
}
|
|
30
34
|
},
|
|
31
35
|
"files": [
|
|
@@ -60,12 +64,12 @@
|
|
|
60
64
|
"tsup": "^8",
|
|
61
65
|
"typescript": "^5.7",
|
|
62
66
|
"vitest": "^4.1.0",
|
|
63
|
-
"@cfast/
|
|
64
|
-
"@cfast/
|
|
67
|
+
"@cfast/env": "0.2.1",
|
|
68
|
+
"@cfast/permissions": "0.5.1"
|
|
65
69
|
},
|
|
66
70
|
"scripts": {
|
|
67
|
-
"build": "tsup src/index.ts src/client/index.ts --format esm --dts",
|
|
68
|
-
"dev": "tsup src/index.ts src/client/index.ts --format esm --dts --watch",
|
|
71
|
+
"build": "tsup src/index.ts src/client/index.ts src/presets.ts --format esm --dts",
|
|
72
|
+
"dev": "tsup src/index.ts src/client/index.ts src/presets.ts --format esm --dts --watch",
|
|
69
73
|
"typecheck": "tsc --noEmit",
|
|
70
74
|
"lint": "eslint src/",
|
|
71
75
|
"test": "vitest run --passWithNoTests"
|