strapi-typed-client 2.1.1 → 2.2.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/README.md +5 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/plugin/server/src/config/index.js +36 -0
- package/dist/cjs/plugin/server/src/controllers/schema.js +130 -0
- package/dist/cjs/plugin/server/src/index.js +93 -0
- package/dist/cjs/plugin/server/src/routes/index.js +32 -0
- package/dist/cjs/plugin/server/src/services/endpoints.js +632 -0
- package/dist/cjs/plugin/server/src/services/schema.js +187 -0
- package/dist/cjs/shared/constants.js +91 -0
- package/dist/cjs/shared/endpoint-types.js +6 -0
- package/dist/cjs/shared/schema-hash.js +105 -0
- package/dist/cjs/shared/strapi-schema-types.js +10 -0
- package/dist/cjs/strapi-server.js +3 -0
- package/dist/cli/commands/generate.d.ts.map +1 -1
- package/dist/cli/commands/generate.js +7 -2
- package/dist/cli/commands/generate.js.map +1 -1
- package/dist/cli/commands/watch.d.ts +1 -0
- package/dist/cli/commands/watch.d.ts.map +1 -1
- package/dist/cli/commands/watch.js +8 -0
- package/dist/cli/commands/watch.js.map +1 -1
- package/dist/generator/client-generator.d.ts.map +1 -1
- package/dist/generator/client-generator.js +3 -0
- package/dist/generator/client-generator.js.map +1 -1
- package/dist/generator/plugin-registry.d.ts.map +1 -1
- package/dist/generator/plugin-registry.js +15 -0
- package/dist/generator/plugin-registry.js.map +1 -1
- package/dist/generator/types-generator.d.ts.map +1 -1
- package/dist/generator/types-generator.js +15 -0
- package/dist/generator/types-generator.js.map +1 -1
- package/dist/next/index.d.ts.map +1 -1
- package/dist/next/index.js +3 -3
- package/dist/next/index.js.map +1 -1
- package/dist/shared/client-header.d.ts +12 -0
- package/dist/shared/client-header.d.ts.map +1 -1
- package/dist/shared/client-header.js +24 -1
- package/dist/shared/client-header.js.map +1 -1
- package/package.json +12 -5
package/README.md
CHANGED
|
@@ -64,6 +64,7 @@ articles[0].category.name // fully typed
|
|
|
64
64
|
- Clean, flat TypeScript interfaces from your Strapi schema
|
|
65
65
|
- Typed API client — `find`, `findOne`, `create`, `update`, `delete`
|
|
66
66
|
- Built-in upload plugin support — `client.upload.upload/find/findOne/destroy`
|
|
67
|
+
- Built-in i18n support — `client.i18n.locales()` for the configured locales
|
|
67
68
|
- Typed errors with `isStrapiErrorOf` for discriminated narrowing
|
|
68
69
|
- Session auth (Strapi 5.43+ `jwtManagement: 'refresh'`) — auto-detected, with transparent token refresh
|
|
69
70
|
- Automatic type inference for `populate` — no manual casting
|
|
@@ -138,6 +139,10 @@ Found a bug or have a feature request? [Open an issue](https://github.com/BoxLab
|
|
|
138
139
|
|
|
139
140
|
Pull requests are welcome — please open an issue first to discuss what you'd like to change.
|
|
140
141
|
|
|
142
|
+
## Support
|
|
143
|
+
|
|
144
|
+
The project is MIT-licensed and free to use. If it saved your team time, there are [a few ways to support it](https://boxlab-ltd.github.io/strapi-typed-client/support) — starring the repo, reporting bugs, or a donation. Teams that need priority fixes or help with integration can reach commercial support at [oss@dearonski.com](mailto:oss@dearonski.com).
|
|
145
|
+
|
|
141
146
|
## License
|
|
142
147
|
|
|
143
148
|
MIT
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for Strapi Types Plugin
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = {
|
|
7
|
+
default: {
|
|
8
|
+
/**
|
|
9
|
+
* Whether authentication is required to access schema endpoints
|
|
10
|
+
* By default: false in development, true in production
|
|
11
|
+
*
|
|
12
|
+
* Can be overridden in config/plugins.ts:
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* export default {
|
|
16
|
+
* 'strapi-types': {
|
|
17
|
+
* enabled: true,
|
|
18
|
+
* config: {
|
|
19
|
+
* requireAuth: false, // Allow unauthenticated access
|
|
20
|
+
* },
|
|
21
|
+
* },
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
requireAuth: process.env.NODE_ENV === 'production',
|
|
26
|
+
},
|
|
27
|
+
/**
|
|
28
|
+
* Validator for plugin configuration
|
|
29
|
+
*/
|
|
30
|
+
validator(config) {
|
|
31
|
+
if (typeof config.requireAuth !== 'undefined' &&
|
|
32
|
+
typeof config.requireAuth !== 'boolean') {
|
|
33
|
+
throw new Error('strapi-types: config.requireAuth must be a boolean');
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Schema controller for Strapi Types Plugin
|
|
4
|
+
* Exposes endpoints for fetching schema, schema hash, and SSE schema watch
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const stream_1 = require("stream");
|
|
8
|
+
/**
|
|
9
|
+
* Manually validate an API token since routes use auth: false
|
|
10
|
+
* (Strapi skips its auth middleware when auth: false is set)
|
|
11
|
+
*/
|
|
12
|
+
async function validateApiToken(strapi, ctx) {
|
|
13
|
+
// Already authenticated via other means
|
|
14
|
+
if (ctx.state.user)
|
|
15
|
+
return true;
|
|
16
|
+
const authorization = ctx.request.header.authorization;
|
|
17
|
+
if (!authorization)
|
|
18
|
+
return false;
|
|
19
|
+
const [scheme, token] = authorization.split(' ');
|
|
20
|
+
if (scheme !== 'Bearer' || !token)
|
|
21
|
+
return false;
|
|
22
|
+
try {
|
|
23
|
+
const apiTokenService = strapi.service('admin::api-token');
|
|
24
|
+
const accessKey = await apiTokenService.hash(token);
|
|
25
|
+
const storedToken = typeof apiTokenService.getByAccessKey === 'function'
|
|
26
|
+
? await apiTokenService.getByAccessKey(accessKey)
|
|
27
|
+
: await apiTokenService.getBy({ accessKey });
|
|
28
|
+
return !!storedToken;
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
strapi.log?.warn?.('[strapi-typed-client] token validation failed', error);
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.default = ({ strapi }) => ({
|
|
36
|
+
/**
|
|
37
|
+
* GET /api/strapi-typed-client/schema
|
|
38
|
+
* Returns the full schema with hash
|
|
39
|
+
*/
|
|
40
|
+
async getSchema(ctx) {
|
|
41
|
+
const requireAuth = strapi.config.get('plugin::strapi-typed-client.requireAuth', process.env.NODE_ENV === 'production');
|
|
42
|
+
if (requireAuth) {
|
|
43
|
+
const isAuthenticated = await validateApiToken(strapi, ctx);
|
|
44
|
+
if (!isAuthenticated) {
|
|
45
|
+
return ctx.unauthorized('Authentication required to access schema');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const schemaService = strapi
|
|
50
|
+
.plugin('strapi-typed-client')
|
|
51
|
+
.service('schema');
|
|
52
|
+
const result = schemaService.getSchema();
|
|
53
|
+
ctx.body = result;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
strapi.log.error('Error extracting schema:', error);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
/**
|
|
61
|
+
* GET /api/strapi-typed-client/schema-hash
|
|
62
|
+
* Returns only the schema hash (lightweight)
|
|
63
|
+
*/
|
|
64
|
+
async getSchemaHash(ctx) {
|
|
65
|
+
const requireAuth = strapi.config.get('plugin::strapi-typed-client.requireAuth', process.env.NODE_ENV === 'production');
|
|
66
|
+
if (requireAuth) {
|
|
67
|
+
const isAuthenticated = await validateApiToken(strapi, ctx);
|
|
68
|
+
if (!isAuthenticated) {
|
|
69
|
+
return ctx.unauthorized('Authentication required to access schema hash');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const schemaService = strapi
|
|
74
|
+
.plugin('strapi-typed-client')
|
|
75
|
+
.service('schema');
|
|
76
|
+
const result = schemaService.getSchemaHash();
|
|
77
|
+
ctx.body = result;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
strapi.log.error('Error computing schema hash:', error);
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* GET /api/strapi-typed-client/schema-watch
|
|
86
|
+
* SSE stream that sends the current hash on connect.
|
|
87
|
+
* When Strapi restarts the connection drops — the client
|
|
88
|
+
* reconnects and gets the (possibly new) hash automatically.
|
|
89
|
+
*/
|
|
90
|
+
async schemaWatch(ctx) {
|
|
91
|
+
const requireAuth = strapi.config.get('plugin::strapi-typed-client.requireAuth', process.env.NODE_ENV === 'production');
|
|
92
|
+
if (requireAuth) {
|
|
93
|
+
const isAuthenticated = await validateApiToken(strapi, ctx);
|
|
94
|
+
if (!isAuthenticated) {
|
|
95
|
+
return ctx.unauthorized('Authentication required to access schema watch');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// SSE headers
|
|
99
|
+
ctx.set('Content-Type', 'text/event-stream');
|
|
100
|
+
ctx.set('Cache-Control', 'no-cache');
|
|
101
|
+
ctx.set('Connection', 'keep-alive');
|
|
102
|
+
ctx.status = 200;
|
|
103
|
+
const stream = new stream_1.PassThrough();
|
|
104
|
+
ctx.body = stream;
|
|
105
|
+
// Send the current hash immediately
|
|
106
|
+
try {
|
|
107
|
+
const schemaService = strapi
|
|
108
|
+
.plugin('strapi-typed-client')
|
|
109
|
+
.service('schema');
|
|
110
|
+
const { hash, generatedAt } = schemaService.getSchemaHash();
|
|
111
|
+
stream.write(`retry: 1000\n`);
|
|
112
|
+
stream.write(`event: connected\n`);
|
|
113
|
+
stream.write(`data: ${JSON.stringify({ hash, generatedAt })}\n\n`);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
strapi.log.error('Error sending initial SSE hash:', error);
|
|
117
|
+
stream.end();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// Heartbeat to keep the connection alive
|
|
121
|
+
const heartbeat = setInterval(() => {
|
|
122
|
+
stream.write(`: heartbeat\n\n`);
|
|
123
|
+
}, 30_000);
|
|
124
|
+
// Cleanup when client disconnects
|
|
125
|
+
ctx.req.on('close', () => {
|
|
126
|
+
clearInterval(heartbeat);
|
|
127
|
+
stream.end();
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Strapi Types Plugin
|
|
4
|
+
*
|
|
5
|
+
* Exposes Strapi schema through REST API for automatic TypeScript type generation.
|
|
6
|
+
*
|
|
7
|
+
* Endpoints:
|
|
8
|
+
* - GET /api/strapi-typed-client/schema - Returns full schema with hash
|
|
9
|
+
* - GET /api/strapi-typed-client/schema-hash - Returns only hash (lightweight)
|
|
10
|
+
*
|
|
11
|
+
* Usage in Strapi:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // config/plugins.ts
|
|
14
|
+
* export default {
|
|
15
|
+
* 'strapi-typed-client': {
|
|
16
|
+
* enabled: true,
|
|
17
|
+
* config: {
|
|
18
|
+
* requireAuth: false, // Optional: allow unauthenticated access
|
|
19
|
+
* },
|
|
20
|
+
* },
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @packageDocumentation
|
|
25
|
+
*/
|
|
26
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
const index_js_1 = __importDefault(require("./config/index.js"));
|
|
31
|
+
const schema_js_1 = __importDefault(require("./controllers/schema.js"));
|
|
32
|
+
const schema_js_2 = __importDefault(require("./services/schema.js"));
|
|
33
|
+
const endpoints_js_1 = __importDefault(require("./services/endpoints.js"));
|
|
34
|
+
const index_js_2 = __importDefault(require("./routes/index.js"));
|
|
35
|
+
exports.default = () => ({
|
|
36
|
+
/**
|
|
37
|
+
* Register phase - runs before bootstrap
|
|
38
|
+
*/
|
|
39
|
+
register({ strapi: _strapi }) {
|
|
40
|
+
// Nothing to register
|
|
41
|
+
},
|
|
42
|
+
/**
|
|
43
|
+
* Bootstrap phase - runs after all plugins are registered
|
|
44
|
+
*/
|
|
45
|
+
bootstrap({ strapi }) {
|
|
46
|
+
strapi.log.info('strapi-types plugin loaded');
|
|
47
|
+
},
|
|
48
|
+
/**
|
|
49
|
+
* Destroy phase - cleanup
|
|
50
|
+
*/
|
|
51
|
+
destroy({ strapi: _strapi }) {
|
|
52
|
+
// Nothing to cleanup
|
|
53
|
+
},
|
|
54
|
+
/**
|
|
55
|
+
* Plugin configuration
|
|
56
|
+
*/
|
|
57
|
+
config: index_js_1.default,
|
|
58
|
+
/**
|
|
59
|
+
* Controllers
|
|
60
|
+
*/
|
|
61
|
+
controllers: {
|
|
62
|
+
schema: schema_js_1.default,
|
|
63
|
+
},
|
|
64
|
+
/**
|
|
65
|
+
* Services
|
|
66
|
+
*/
|
|
67
|
+
services: {
|
|
68
|
+
schema: schema_js_2.default,
|
|
69
|
+
endpoints: endpoints_js_1.default,
|
|
70
|
+
},
|
|
71
|
+
/**
|
|
72
|
+
* Routes
|
|
73
|
+
*/
|
|
74
|
+
routes: {
|
|
75
|
+
'content-api': {
|
|
76
|
+
type: 'content-api',
|
|
77
|
+
routes: index_js_2.default,
|
|
78
|
+
},
|
|
79
|
+
admin: {
|
|
80
|
+
type: 'admin',
|
|
81
|
+
routes: [
|
|
82
|
+
{
|
|
83
|
+
method: 'GET',
|
|
84
|
+
path: '/schema',
|
|
85
|
+
handler: 'schema.getSchema',
|
|
86
|
+
config: {
|
|
87
|
+
policies: [],
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Routes for Strapi Types Plugin
|
|
4
|
+
* Defines content-api routes for schema access
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.default = [
|
|
8
|
+
{
|
|
9
|
+
method: 'GET',
|
|
10
|
+
path: '/schema',
|
|
11
|
+
handler: 'schema.getSchema',
|
|
12
|
+
config: {
|
|
13
|
+
auth: false,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
method: 'GET',
|
|
18
|
+
path: '/schema-hash',
|
|
19
|
+
handler: 'schema.getSchemaHash',
|
|
20
|
+
config: {
|
|
21
|
+
auth: false,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
method: 'GET',
|
|
26
|
+
path: '/schema-watch',
|
|
27
|
+
handler: 'schema.schemaWatch',
|
|
28
|
+
config: {
|
|
29
|
+
auth: false,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
];
|