@zenstackhq/swr 1.0.0-beta.8 → 1.0.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/generator.d.ts +1 -1
- package/generator.js +66 -59
- package/generator.js.map +1 -1
- package/package.json +12 -12
- package/runtime/index.d.ts +106 -0
- package/runtime/index.js +235 -0
- package/runtime/index.js.map +1 -0
- package/runtime/prisma-types.d.ts +17 -0
- package/runtime/prisma-types.js +5 -0
- package/runtime/prisma-types.js.map +1 -0
- package/res/helper.ts +0 -177
- package/res/marshal-json.ts +0 -12
- package/res/marshal-superjson.ts +0 -20
package/generator.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper';
|
|
1
|
+
import type { DMMF } from '@prisma/generator-helper';
|
|
2
2
|
import { PluginOptions } from '@zenstackhq/sdk';
|
|
3
3
|
import { Model } from '@zenstackhq/sdk/ast';
|
|
4
4
|
export declare function generate(model: Model, options: PluginOptions, dmmf: DMMF.Document): Promise<string[]>;
|
package/generator.js
CHANGED
|
@@ -15,9 +15,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.generate = void 0;
|
|
16
16
|
const sdk_1 = require("@zenstackhq/sdk");
|
|
17
17
|
const change_case_1 = require("change-case");
|
|
18
|
-
const fs_1 = __importDefault(require("fs"));
|
|
19
18
|
const lower_case_first_1 = require("lower-case-first");
|
|
20
19
|
const path_1 = __importDefault(require("path"));
|
|
20
|
+
const semver_1 = __importDefault(require("semver"));
|
|
21
21
|
const upper_case_first_1 = require("upper-case-first");
|
|
22
22
|
function generate(model, options, dmmf) {
|
|
23
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -25,9 +25,11 @@ function generate(model, options, dmmf) {
|
|
|
25
25
|
outDir = (0, sdk_1.resolvePath)(outDir, options);
|
|
26
26
|
const project = (0, sdk_1.createProject)();
|
|
27
27
|
const warnings = [];
|
|
28
|
+
if (options.useSuperJson !== undefined) {
|
|
29
|
+
warnings.push('The option "useSuperJson" is deprecated. The generated hooks always use superjson for serialization.');
|
|
30
|
+
}
|
|
28
31
|
const models = (0, sdk_1.getDataModels)(model);
|
|
29
32
|
generateIndex(project, outDir, models);
|
|
30
|
-
generateHelper(project, outDir, options.useSuperJson === true);
|
|
31
33
|
models.forEach((dataModel) => {
|
|
32
34
|
const mapping = dmmf.mappings.modelOperations.find((op) => op.model === dataModel.name);
|
|
33
35
|
if (!mapping) {
|
|
@@ -41,18 +43,6 @@ function generate(model, options, dmmf) {
|
|
|
41
43
|
});
|
|
42
44
|
}
|
|
43
45
|
exports.generate = generate;
|
|
44
|
-
function wrapReadbackErrorCheck(code) {
|
|
45
|
-
return `try {
|
|
46
|
-
${code}
|
|
47
|
-
} catch (err: any) {
|
|
48
|
-
if (err.info?.prisma && err.info?.code === 'P2004' && err.info?.reason === '${sdk_1.CrudFailureReason.RESULT_NOT_READABLE}') {
|
|
49
|
-
// unable to readback data
|
|
50
|
-
return undefined;
|
|
51
|
-
} else {
|
|
52
|
-
throw err;
|
|
53
|
-
}
|
|
54
|
-
}`;
|
|
55
|
-
}
|
|
56
46
|
function generateModelHooks(project, outDir, model, mapping) {
|
|
57
47
|
const fileName = (0, change_case_1.paramCase)(model.name);
|
|
58
48
|
const sf = project.createSourceFile(path_1.default.join(outDir, `${fileName}.ts`), undefined, { overwrite: true });
|
|
@@ -65,9 +55,11 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
65
55
|
});
|
|
66
56
|
sf.addStatements([
|
|
67
57
|
`import { useContext } from 'react';`,
|
|
68
|
-
`import { RequestHandlerContext, type RequestOptions } from '
|
|
69
|
-
`import * as request from '
|
|
58
|
+
`import { RequestHandlerContext, type GetNextArgs, type RequestOptions, type InfiniteRequestOptions, type PickEnumerable, type CheckSelect } from '@zenstackhq/swr/runtime';`,
|
|
59
|
+
`import * as request from '@zenstackhq/swr/runtime';`,
|
|
70
60
|
]);
|
|
61
|
+
const modelNameCap = (0, upper_case_first_1.upperCaseFirst)(model.name);
|
|
62
|
+
const prismaVersion = (0, sdk_1.getPrismaVersion)();
|
|
71
63
|
const prefixesToMutate = ['find', 'aggregate', 'count', 'groupBy'];
|
|
72
64
|
const useMutation = sf.addFunction({
|
|
73
65
|
name: `useMutate${model.name}`,
|
|
@@ -86,22 +78,25 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
86
78
|
if (mapping.create || mapping.createOne) {
|
|
87
79
|
const argsType = `Prisma.${model.name}CreateArgs`;
|
|
88
80
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
89
|
-
const returnType = `
|
|
90
|
-
mutationFuncs.push(generateMutation(useMutation, model, 'post', 'create', argsType, inputType, returnType));
|
|
81
|
+
const returnType = `CheckSelect<T, ${model.name}, Prisma.${model.name}GetPayload<T>>`;
|
|
82
|
+
mutationFuncs.push(generateMutation(useMutation, model, 'post', 'create', argsType, inputType, returnType, true));
|
|
91
83
|
}
|
|
92
84
|
// createMany
|
|
93
85
|
if (mapping.createMany) {
|
|
94
86
|
const argsType = `Prisma.${model.name}CreateManyArgs`;
|
|
95
87
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
96
88
|
const returnType = `Prisma.BatchPayload`;
|
|
97
|
-
mutationFuncs.push(generateMutation(useMutation, model, 'post', 'createMany', argsType, inputType, returnType));
|
|
89
|
+
mutationFuncs.push(generateMutation(useMutation, model, 'post', 'createMany', argsType, inputType, returnType, false));
|
|
98
90
|
}
|
|
99
91
|
// findMany
|
|
100
92
|
if (mapping.findMany) {
|
|
101
93
|
const argsType = `Prisma.${model.name}FindManyArgs`;
|
|
102
94
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
103
95
|
const returnType = `Array<Prisma.${model.name}GetPayload<T>>`;
|
|
96
|
+
// regular findMany
|
|
104
97
|
generateQueryHook(sf, model, 'findMany', argsType, inputType, returnType);
|
|
98
|
+
// infinite findMany
|
|
99
|
+
generateQueryHook(sf, model, 'findMany', argsType, inputType, returnType, undefined, true);
|
|
105
100
|
}
|
|
106
101
|
// findUnique
|
|
107
102
|
if (mapping.findUnique) {
|
|
@@ -124,14 +119,14 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
124
119
|
const argsType = `Prisma.${model.name}UpdateArgs`;
|
|
125
120
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
126
121
|
const returnType = `Prisma.${model.name}GetPayload<T>`;
|
|
127
|
-
mutationFuncs.push(generateMutation(useMutation, model, 'put', 'update', argsType, inputType, returnType));
|
|
122
|
+
mutationFuncs.push(generateMutation(useMutation, model, 'put', 'update', argsType, inputType, returnType, true));
|
|
128
123
|
}
|
|
129
124
|
// updateMany
|
|
130
125
|
if (mapping.updateMany) {
|
|
131
126
|
const argsType = `Prisma.${model.name}UpdateManyArgs`;
|
|
132
127
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
133
128
|
const returnType = `Prisma.BatchPayload`;
|
|
134
|
-
mutationFuncs.push(generateMutation(useMutation, model, 'put', 'updateMany', argsType, inputType, returnType));
|
|
129
|
+
mutationFuncs.push(generateMutation(useMutation, model, 'put', 'updateMany', argsType, inputType, returnType, false));
|
|
135
130
|
}
|
|
136
131
|
// upsert
|
|
137
132
|
// upsert is somehow named "upsertOne" in the DMMF
|
|
@@ -140,7 +135,7 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
140
135
|
const argsType = `Prisma.${model.name}UpsertArgs`;
|
|
141
136
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
142
137
|
const returnType = `Prisma.${model.name}GetPayload<T>`;
|
|
143
|
-
mutationFuncs.push(generateMutation(useMutation, model, 'post', 'upsert', argsType, inputType, returnType));
|
|
138
|
+
mutationFuncs.push(generateMutation(useMutation, model, 'post', 'upsert', argsType, inputType, returnType, true));
|
|
144
139
|
}
|
|
145
140
|
// del
|
|
146
141
|
// delete is somehow named "deleteOne" in the DMMF
|
|
@@ -149,30 +144,35 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
149
144
|
const argsType = `Prisma.${model.name}DeleteArgs`;
|
|
150
145
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
151
146
|
const returnType = `Prisma.${model.name}GetPayload<T>`;
|
|
152
|
-
mutationFuncs.push(generateMutation(useMutation, model, 'delete', 'delete', argsType, inputType, returnType));
|
|
147
|
+
mutationFuncs.push(generateMutation(useMutation, model, 'delete', 'delete', argsType, inputType, returnType, true));
|
|
153
148
|
}
|
|
154
149
|
// deleteMany
|
|
155
150
|
if (mapping.deleteMany) {
|
|
156
151
|
const argsType = `Prisma.${model.name}DeleteManyArgs`;
|
|
157
152
|
const inputType = `Prisma.SelectSubset<T, ${argsType}>`;
|
|
158
153
|
const returnType = `Prisma.BatchPayload`;
|
|
159
|
-
mutationFuncs.push(generateMutation(useMutation, model, 'delete', 'deleteMany', argsType, inputType, returnType));
|
|
154
|
+
mutationFuncs.push(generateMutation(useMutation, model, 'delete', 'deleteMany', argsType, inputType, returnType, false));
|
|
160
155
|
}
|
|
161
156
|
// aggregate
|
|
162
157
|
if (mapping.aggregate) {
|
|
163
|
-
const argsType = `Prisma.${
|
|
158
|
+
const argsType = `Prisma.${modelNameCap}AggregateArgs`;
|
|
164
159
|
const inputType = `Prisma.Subset<T, ${argsType}>`;
|
|
165
|
-
const returnType = `Prisma.Get${
|
|
160
|
+
const returnType = `Prisma.Get${modelNameCap}AggregateType<T>`;
|
|
166
161
|
generateQueryHook(sf, model, 'aggregate', argsType, inputType, returnType);
|
|
167
162
|
}
|
|
168
163
|
// groupBy
|
|
169
164
|
if (mapping.groupBy) {
|
|
165
|
+
let useName = modelNameCap;
|
|
166
|
+
if (prismaVersion && semver_1.default.gte(prismaVersion, '5.0.0')) {
|
|
167
|
+
// prisma 4 and 5 different typing for "groupBy" and we have to deal with it separately
|
|
168
|
+
useName = model.name;
|
|
169
|
+
}
|
|
170
170
|
const typeParameters = [
|
|
171
|
-
`T extends Prisma.${
|
|
171
|
+
`T extends Prisma.${useName}GroupByArgs`,
|
|
172
172
|
`HasSelectOrTake extends Prisma.Or<Prisma.Extends<'skip', Prisma.Keys<T>>, Prisma.Extends<'take', Prisma.Keys<T>>>`,
|
|
173
|
-
`OrderByArg extends Prisma.True extends HasSelectOrTake ? { orderBy: Prisma.${
|
|
173
|
+
`OrderByArg extends Prisma.True extends HasSelectOrTake ? { orderBy: Prisma.${useName}GroupByArgs['orderBy'] }: { orderBy?: Prisma.${useName}GroupByArgs['orderBy'] },`,
|
|
174
174
|
`OrderFields extends Prisma.ExcludeUnderscoreKeys<Prisma.Keys<Prisma.MaybeTupleToUnion<T['orderBy']>>>`,
|
|
175
|
-
`ByFields extends Prisma.
|
|
175
|
+
`ByFields extends Prisma.MaybeTupleToUnion<T['by']>`,
|
|
176
176
|
`ByValid extends Prisma.Has<ByFields, OrderFields>`,
|
|
177
177
|
`HavingFields extends Prisma.GetHavingFields<T['having']>`,
|
|
178
178
|
`HavingValid extends Prisma.Has<ByFields, HavingFields>`,
|
|
@@ -220,15 +220,15 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
220
220
|
: \`Error: Field "\${P}" in "orderBy" needs to be provided in "by"\`
|
|
221
221
|
}[OrderFields]`,
|
|
222
222
|
];
|
|
223
|
-
const inputType = `Prisma.SubsetIntersection<T, Prisma.${
|
|
223
|
+
const inputType = `Prisma.SubsetIntersection<T, Prisma.${useName}GroupByArgs, OrderByArg> & InputErrors`;
|
|
224
224
|
const returnType = `{} extends InputErrors ?
|
|
225
|
-
Array<
|
|
225
|
+
Array<PickEnumerable<Prisma.${modelNameCap}GroupByOutputType, T['by']> &
|
|
226
226
|
{
|
|
227
|
-
[P in ((keyof T) & (keyof Prisma.${
|
|
227
|
+
[P in ((keyof T) & (keyof Prisma.${modelNameCap}GroupByOutputType))]: P extends '_count'
|
|
228
228
|
? T[P] extends boolean
|
|
229
229
|
? number
|
|
230
|
-
: Prisma.GetScalarType<T[P], Prisma.${
|
|
231
|
-
: Prisma.GetScalarType<T[P], Prisma.${
|
|
230
|
+
: Prisma.GetScalarType<T[P], Prisma.${modelNameCap}GroupByOutputType[P]>
|
|
231
|
+
: Prisma.GetScalarType<T[P], Prisma.${modelNameCap}GroupByOutputType[P]>
|
|
232
232
|
}
|
|
233
233
|
> : InputErrors`;
|
|
234
234
|
generateQueryHook(sf, model, 'groupBy', '', inputType, returnType, typeParameters);
|
|
@@ -237,7 +237,7 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
237
237
|
{
|
|
238
238
|
const argsType = `Prisma.${model.name}CountArgs`;
|
|
239
239
|
const inputType = `Prisma.Subset<T, ${argsType}>`;
|
|
240
|
-
const returnType = `T extends { select: any; } ? T['select'] extends true ? number : Prisma.GetScalarType<T['select'], Prisma.${
|
|
240
|
+
const returnType = `T extends { select: any; } ? T['select'] extends true ? number : Prisma.GetScalarType<T['select'], Prisma.${modelNameCap}CountAggregateOutputType> : number`;
|
|
241
241
|
generateQueryHook(sf, model, 'count', argsType, inputType, returnType);
|
|
242
242
|
}
|
|
243
243
|
useMutation.addStatements(`return { ${mutationFuncs.join(', ')} };`);
|
|
@@ -245,32 +245,46 @@ function generateModelHooks(project, outDir, model, mapping) {
|
|
|
245
245
|
function generateIndex(project, outDir, models) {
|
|
246
246
|
const sf = project.createSourceFile(path_1.default.join(outDir, 'index.ts'), undefined, { overwrite: true });
|
|
247
247
|
sf.addStatements(models.map((d) => `export * from './${(0, change_case_1.paramCase)(d.name)}';`));
|
|
248
|
-
sf.addStatements(`export
|
|
248
|
+
sf.addStatements(`export { Provider } from '@zenstackhq/swr/runtime';`);
|
|
249
249
|
}
|
|
250
|
-
function generateQueryHook(sf, model, operation, argsType, inputType, returnType, typeParameters) {
|
|
250
|
+
function generateQueryHook(sf, model, operation, argsType, inputType, returnType, typeParameters, infinite = false) {
|
|
251
251
|
const modelRouteName = (0, lower_case_first_1.lowerCaseFirst)(model.name);
|
|
252
|
+
const typeParams = typeParameters ? [...typeParameters] : [`T extends ${argsType}`];
|
|
253
|
+
if (infinite) {
|
|
254
|
+
typeParams.push(`R extends ${returnType}`);
|
|
255
|
+
}
|
|
256
|
+
const parameters = [];
|
|
257
|
+
if (!infinite) {
|
|
258
|
+
parameters.push({
|
|
259
|
+
name: 'args?',
|
|
260
|
+
type: inputType,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
parameters.push({
|
|
265
|
+
name: 'getNextArgs',
|
|
266
|
+
type: `GetNextArgs<${inputType} | undefined, R>`,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
parameters.push({
|
|
270
|
+
name: 'options?',
|
|
271
|
+
type: infinite ? `InfiniteRequestOptions<${returnType}>` : `RequestOptions<${returnType}>`,
|
|
272
|
+
});
|
|
252
273
|
sf.addFunction({
|
|
253
|
-
name: `use${(0, upper_case_first_1.upperCaseFirst)(operation)}${model.name}`,
|
|
254
|
-
typeParameters:
|
|
274
|
+
name: `use${infinite ? 'Infinite' : ''}${(0, upper_case_first_1.upperCaseFirst)(operation)}${model.name}`,
|
|
275
|
+
typeParameters: typeParams,
|
|
255
276
|
isExported: true,
|
|
256
|
-
parameters
|
|
257
|
-
{
|
|
258
|
-
name: 'args?',
|
|
259
|
-
type: inputType,
|
|
260
|
-
},
|
|
261
|
-
{
|
|
262
|
-
name: 'options?',
|
|
263
|
-
type: `RequestOptions<${returnType}>`,
|
|
264
|
-
},
|
|
265
|
-
],
|
|
277
|
+
parameters,
|
|
266
278
|
})
|
|
267
279
|
.addBody()
|
|
268
280
|
.addStatements([
|
|
269
281
|
'const { endpoint, fetch } = useContext(RequestHandlerContext);',
|
|
270
|
-
|
|
282
|
+
!infinite
|
|
283
|
+
? `return request.get<${returnType}>(\`\${endpoint}/${modelRouteName}/${operation}\`, args, options, fetch);`
|
|
284
|
+
: `return request.infiniteGet<${inputType} | undefined, ${returnType}>(\`\${endpoint}/${modelRouteName}/${operation}\`, getNextArgs, options, fetch);`,
|
|
271
285
|
]);
|
|
272
286
|
}
|
|
273
|
-
function generateMutation(func, model, method, operation, argsType, inputType, returnType) {
|
|
287
|
+
function generateMutation(func, model, method, operation, argsType, inputType, returnType, checkReadBack) {
|
|
274
288
|
const modelRouteName = (0, lower_case_first_1.lowerCaseFirst)(model.name);
|
|
275
289
|
const funcName = `${operation}${model.name}`;
|
|
276
290
|
const fetcherFunc = method === 'delete' ? 'del' : method;
|
|
@@ -287,15 +301,8 @@ function generateMutation(func, model, method, operation, argsType, inputType, r
|
|
|
287
301
|
})
|
|
288
302
|
.addBody()
|
|
289
303
|
.addStatements([
|
|
290
|
-
|
|
304
|
+
`return await request.${fetcherFunc}<${returnType}, ${checkReadBack}>(\`\${endpoint}/${modelRouteName}/${operation}\`, args, mutate, fetch, ${checkReadBack});`,
|
|
291
305
|
]);
|
|
292
306
|
return funcName;
|
|
293
307
|
}
|
|
294
|
-
function generateHelper(project, outDir, useSuperJson) {
|
|
295
|
-
const helperContent = fs_1.default.readFileSync(path_1.default.join(__dirname, './res/helper.ts'), 'utf-8');
|
|
296
|
-
const marshalContent = fs_1.default.readFileSync(path_1.default.join(__dirname, useSuperJson ? './res/marshal-superjson.ts' : './res/marshal-json.ts'), 'utf-8');
|
|
297
|
-
project.createSourceFile(path_1.default.join(outDir, '_helper.ts'), `${helperContent}\n${marshalContent}`, {
|
|
298
|
-
overwrite: true,
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
308
|
//# sourceMappingURL=generator.js.map
|
package/generator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,yCASyB;AAEzB,6CAAwC;AACxC,
|
|
1
|
+
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,yCASyB;AAEzB,6CAAwC;AACxC,uDAAkD;AAClD,gDAAwB;AAC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EAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClD,MAAM,QAAQ,GAAG,GAAG,SAAS,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7C,MAAM,WAAW,GAAG,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC;IACzD,IAAI,CAAC,WAAW,CAAC;QACb,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,IAAI;QACb,cAAc,EAAE,CAAC,aAAa,QAAQ,EAAE,CAAC;QACzC,UAAU,EAAE;YACR;gBACI,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,SAAS;aAClB;SACJ;KACJ,CAAC;SACG,OAAO,EAAE;SACT,aAAa,CAAC;QACX,wBAAwB,WAAW,IAAI,UAAU,KAAK,aAAa,oBAAoB,cAAc,IAAI,SAAS,4BAA4B,aAAa,IAAI;KAClK,CAAC,CAAC;IACP,OAAO,QAAQ,CAAC;AACpB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenstackhq/swr",
|
|
3
3
|
"displayName": "ZenStack plugin for generating SWR hooks",
|
|
4
|
-
"version": "1.0.0
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"description": "ZenStack plugin for generating SWR hooks",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"repository": {
|
|
@@ -16,38 +16,38 @@
|
|
|
16
16
|
"author": "ZenStack Team",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@prisma/generator-helper": "
|
|
19
|
+
"@prisma/generator-helper": "^5.0.0",
|
|
20
20
|
"change-case": "^4.1.2",
|
|
21
21
|
"decimal.js": "^10.4.2",
|
|
22
22
|
"lower-case-first": "^2.0.2",
|
|
23
|
-
"
|
|
23
|
+
"semver": "^7.3.8",
|
|
24
24
|
"ts-morph": "^16.0.0",
|
|
25
25
|
"upper-case-first": "^2.0.2",
|
|
26
|
-
"@zenstackhq/sdk": "1.0.0
|
|
26
|
+
"@zenstackhq/sdk": "1.0.0",
|
|
27
|
+
"@zenstackhq/runtime": "1.0.0"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@tanstack/react-query": "^4.28.0",
|
|
30
31
|
"@types/jest": "^29.5.0",
|
|
31
|
-
"@types/
|
|
32
|
-
"@types/react": "
|
|
32
|
+
"@types/node": "^18.0.0",
|
|
33
|
+
"@types/react": "18.2.0",
|
|
34
|
+
"@types/semver": "^7.3.13",
|
|
33
35
|
"@types/tmp": "^0.2.3",
|
|
34
|
-
"@types/upper-case-first": "^1.1.2",
|
|
35
36
|
"copyfiles": "^2.4.1",
|
|
36
37
|
"jest": "^29.5.0",
|
|
37
|
-
"react": "
|
|
38
|
-
"react-dom": "^17.0.2 || ^18",
|
|
38
|
+
"react": "18.2.0",
|
|
39
39
|
"rimraf": "^3.0.2",
|
|
40
40
|
"swr": "^2.0.3",
|
|
41
41
|
"ts-jest": "^29.0.5",
|
|
42
42
|
"typescript": "^4.9.4",
|
|
43
|
-
"@zenstackhq/testtools": "1.0.0
|
|
43
|
+
"@zenstackhq/testtools": "1.0.0"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"clean": "rimraf dist",
|
|
47
|
-
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE
|
|
47
|
+
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE dist && pnpm pack dist --pack-destination '../../../../.build'",
|
|
48
48
|
"watch": "tsc --watch",
|
|
49
49
|
"lint": "eslint src --ext ts",
|
|
50
|
-
"test": "jest",
|
|
50
|
+
"test": "ZENSTACK_TEST=1 jest",
|
|
51
51
|
"publish-dev": "pnpm publish --tag dev"
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { Fetcher, MutatorCallback, MutatorOptions, SWRConfiguration, SWRResponse } from 'swr';
|
|
3
|
+
import { SWRInfiniteConfiguration, SWRInfiniteFetcher, SWRInfiniteResponse } from 'swr/infinite';
|
|
4
|
+
export * from './prisma-types';
|
|
5
|
+
/**
|
|
6
|
+
* Function signature for `fetch`.
|
|
7
|
+
*/
|
|
8
|
+
export type FetchFn = (url: string, options?: RequestInit) => Promise<Response>;
|
|
9
|
+
/**
|
|
10
|
+
* Context type for configuring react hooks.
|
|
11
|
+
*/
|
|
12
|
+
export type RequestHandlerContext = {
|
|
13
|
+
/**
|
|
14
|
+
* The endpoint to use for the queries.
|
|
15
|
+
*/
|
|
16
|
+
endpoint: string;
|
|
17
|
+
/**
|
|
18
|
+
* A custom fetch function for sending the HTTP requests.
|
|
19
|
+
*/
|
|
20
|
+
fetch?: FetchFn;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Context for configuring react hooks.
|
|
24
|
+
*/
|
|
25
|
+
export declare const RequestHandlerContext: import("react").Context<RequestHandlerContext>;
|
|
26
|
+
/**
|
|
27
|
+
* Context provider.
|
|
28
|
+
*/
|
|
29
|
+
export declare const Provider: import("react").Provider<RequestHandlerContext>;
|
|
30
|
+
/**
|
|
31
|
+
* Client request options for regular query.
|
|
32
|
+
*/
|
|
33
|
+
export type RequestOptions<Result, Error = any> = {
|
|
34
|
+
/**
|
|
35
|
+
* Disable data fetching
|
|
36
|
+
*/
|
|
37
|
+
disabled?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Equivalent to @see SWRConfiguration.fallbackData
|
|
40
|
+
*/
|
|
41
|
+
initialData?: Result;
|
|
42
|
+
} & SWRConfiguration<Result, Error, Fetcher<Result>>;
|
|
43
|
+
/**
|
|
44
|
+
* Client request options for infinite query.
|
|
45
|
+
*/
|
|
46
|
+
export type InfiniteRequestOptions<Result, Error = any> = {
|
|
47
|
+
/**
|
|
48
|
+
* Disable data fetching
|
|
49
|
+
*/
|
|
50
|
+
disabled?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Equivalent to @see SWRInfiniteConfiguration.fallbackData
|
|
53
|
+
*/
|
|
54
|
+
initialData?: Result[];
|
|
55
|
+
} & SWRInfiniteConfiguration<Result, Error, SWRInfiniteFetcher<Result>>;
|
|
56
|
+
/**
|
|
57
|
+
* Makes a GET request with SWR.
|
|
58
|
+
*
|
|
59
|
+
* @param url The request URL.
|
|
60
|
+
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
|
|
61
|
+
* @param options Query options
|
|
62
|
+
* @param fetch Custom fetch function
|
|
63
|
+
* @returns SWR response
|
|
64
|
+
*/
|
|
65
|
+
export declare function get<Result, Error = any>(url: string | null, args?: unknown, options?: RequestOptions<Result, Error>, fetch?: FetchFn): SWRResponse<Result, Error>;
|
|
66
|
+
/**
|
|
67
|
+
* Function for computing the query args for fetching a page during an infinite query.
|
|
68
|
+
*/
|
|
69
|
+
export type GetNextArgs<Args, Result> = (pageIndex: number, previousPageData: Result | null) => Args | null;
|
|
70
|
+
/**
|
|
71
|
+
* Makes an infinite GET request with SWR.
|
|
72
|
+
*
|
|
73
|
+
* @param url The request URL.
|
|
74
|
+
* @param getNextArgs Function for computing the query args for a page.
|
|
75
|
+
* @param options Query options
|
|
76
|
+
* @param fetch Custom fetch function
|
|
77
|
+
* @returns SWR infinite query response
|
|
78
|
+
*/
|
|
79
|
+
export declare function infiniteGet<Args, Result, Error = any>(url: string | null, getNextArgs: GetNextArgs<Args, any>, options?: InfiniteRequestOptions<Result, Error>, fetch?: FetchFn): SWRInfiniteResponse<Result, Error>;
|
|
80
|
+
/**
|
|
81
|
+
* Makes a POST request.
|
|
82
|
+
*
|
|
83
|
+
* @param url The request URL.
|
|
84
|
+
* @param data The request data.
|
|
85
|
+
* @param mutate Mutator for invalidating cache.
|
|
86
|
+
*/
|
|
87
|
+
export declare function post<Result, C extends boolean = boolean>(url: string, data: unknown, mutate: Mutator, fetch?: FetchFn, checkReadBack?: C): Promise<C extends true ? Result | undefined : Result>;
|
|
88
|
+
/**
|
|
89
|
+
* Makes a PUT request.
|
|
90
|
+
*
|
|
91
|
+
* @param url The request URL.
|
|
92
|
+
* @param data The request data.
|
|
93
|
+
* @param mutate Mutator for invalidating cache.
|
|
94
|
+
*/
|
|
95
|
+
export declare function put<Result, C extends boolean = boolean>(url: string, data: unknown, mutate: Mutator, fetch?: FetchFn, checkReadBack?: C): Promise<C extends true ? Result | undefined : Result>;
|
|
96
|
+
/**
|
|
97
|
+
* Makes a DELETE request.
|
|
98
|
+
*
|
|
99
|
+
* @param url The request URL.
|
|
100
|
+
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
|
|
101
|
+
* @param mutate Mutator for invalidating cache.
|
|
102
|
+
*/
|
|
103
|
+
export declare function del<Result, C extends boolean = boolean>(url: string, args: unknown, mutate: Mutator, fetch?: FetchFn, checkReadBack?: C): Promise<C extends true ? Result | undefined : Result>;
|
|
104
|
+
type Mutator = (data?: unknown | Promise<unknown> | MutatorCallback, opts?: boolean | MutatorOptions) => Promise<unknown[]>;
|
|
105
|
+
export declare function getMutate(prefixes: string[]): Mutator;
|
|
106
|
+
export declare function fetcher<R, C extends boolean>(url: string, options?: RequestInit, fetch?: FetchFn, checkReadBack?: C): Promise<C extends true ? R | undefined : R>;
|
package/runtime/index.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
38
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.fetcher = exports.getMutate = exports.del = exports.put = exports.post = exports.infiniteGet = exports.get = exports.Provider = exports.RequestHandlerContext = void 0;
|
|
42
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
43
|
+
const browser_1 = require("@zenstackhq/runtime/browser");
|
|
44
|
+
const react_1 = require("react");
|
|
45
|
+
const swr_1 = __importStar(require("swr"));
|
|
46
|
+
const infinite_1 = __importDefault(require("swr/infinite"));
|
|
47
|
+
__exportStar(require("./prisma-types"), exports);
|
|
48
|
+
/**
|
|
49
|
+
* Context for configuring react hooks.
|
|
50
|
+
*/
|
|
51
|
+
exports.RequestHandlerContext = (0, react_1.createContext)({
|
|
52
|
+
endpoint: '/api/model',
|
|
53
|
+
fetch: undefined,
|
|
54
|
+
});
|
|
55
|
+
/**
|
|
56
|
+
* Context provider.
|
|
57
|
+
*/
|
|
58
|
+
exports.Provider = exports.RequestHandlerContext.Provider;
|
|
59
|
+
/**
|
|
60
|
+
* Makes a GET request with SWR.
|
|
61
|
+
*
|
|
62
|
+
* @param url The request URL.
|
|
63
|
+
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
|
|
64
|
+
* @param options Query options
|
|
65
|
+
* @param fetch Custom fetch function
|
|
66
|
+
* @returns SWR response
|
|
67
|
+
*/
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
69
|
+
function get(url, args, options, fetch) {
|
|
70
|
+
var _a;
|
|
71
|
+
const reqUrl = (options === null || options === void 0 ? void 0 : options.disabled) ? null : url ? makeUrl(url, args) : null;
|
|
72
|
+
return (0, swr_1.default)(reqUrl, (url) => fetcher(url, undefined, fetch, false), Object.assign(Object.assign({}, options), { fallbackData: (_a = options === null || options === void 0 ? void 0 : options.initialData) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.fallbackData }));
|
|
73
|
+
}
|
|
74
|
+
exports.get = get;
|
|
75
|
+
/**
|
|
76
|
+
* Makes an infinite GET request with SWR.
|
|
77
|
+
*
|
|
78
|
+
* @param url The request URL.
|
|
79
|
+
* @param getNextArgs Function for computing the query args for a page.
|
|
80
|
+
* @param options Query options
|
|
81
|
+
* @param fetch Custom fetch function
|
|
82
|
+
* @returns SWR infinite query response
|
|
83
|
+
*/
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
|
+
function infiniteGet(url, getNextArgs, options, fetch) {
|
|
86
|
+
var _a;
|
|
87
|
+
const getKey = (pageIndex, previousPageData) => {
|
|
88
|
+
if ((options === null || options === void 0 ? void 0 : options.disabled) || !url) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const nextArgs = getNextArgs(pageIndex, previousPageData);
|
|
92
|
+
return nextArgs !== null // null means reached the end
|
|
93
|
+
? makeUrl(url, nextArgs)
|
|
94
|
+
: null;
|
|
95
|
+
};
|
|
96
|
+
return (0, infinite_1.default)(getKey, (url) => fetcher(url, undefined, fetch, false), Object.assign(Object.assign({}, options), { fallbackData: (_a = options === null || options === void 0 ? void 0 : options.initialData) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.fallbackData }));
|
|
97
|
+
}
|
|
98
|
+
exports.infiniteGet = infiniteGet;
|
|
99
|
+
/**
|
|
100
|
+
* Makes a POST request.
|
|
101
|
+
*
|
|
102
|
+
* @param url The request URL.
|
|
103
|
+
* @param data The request data.
|
|
104
|
+
* @param mutate Mutator for invalidating cache.
|
|
105
|
+
*/
|
|
106
|
+
function post(url, data, mutate, fetch, checkReadBack) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const r = yield fetcher(url, {
|
|
109
|
+
method: 'POST',
|
|
110
|
+
headers: {
|
|
111
|
+
'content-type': 'application/json',
|
|
112
|
+
},
|
|
113
|
+
body: marshal(data),
|
|
114
|
+
}, fetch, checkReadBack);
|
|
115
|
+
mutate();
|
|
116
|
+
return r;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
exports.post = post;
|
|
120
|
+
/**
|
|
121
|
+
* Makes a PUT request.
|
|
122
|
+
*
|
|
123
|
+
* @param url The request URL.
|
|
124
|
+
* @param data The request data.
|
|
125
|
+
* @param mutate Mutator for invalidating cache.
|
|
126
|
+
*/
|
|
127
|
+
function put(url, data, mutate, fetch, checkReadBack) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
const r = yield fetcher(url, {
|
|
130
|
+
method: 'PUT',
|
|
131
|
+
headers: {
|
|
132
|
+
'content-type': 'application/json',
|
|
133
|
+
},
|
|
134
|
+
body: marshal(data),
|
|
135
|
+
}, fetch, checkReadBack);
|
|
136
|
+
mutate();
|
|
137
|
+
return r;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
exports.put = put;
|
|
141
|
+
/**
|
|
142
|
+
* Makes a DELETE request.
|
|
143
|
+
*
|
|
144
|
+
* @param url The request URL.
|
|
145
|
+
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
|
|
146
|
+
* @param mutate Mutator for invalidating cache.
|
|
147
|
+
*/
|
|
148
|
+
function del(url, args, mutate, fetch, checkReadBack) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
const reqUrl = makeUrl(url, args);
|
|
151
|
+
const r = yield fetcher(reqUrl, {
|
|
152
|
+
method: 'DELETE',
|
|
153
|
+
}, fetch, checkReadBack);
|
|
154
|
+
const path = url.split('/');
|
|
155
|
+
path.pop();
|
|
156
|
+
mutate();
|
|
157
|
+
return r;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
exports.del = del;
|
|
161
|
+
function getMutate(prefixes) {
|
|
162
|
+
// https://swr.vercel.app/docs/advanced/cache#mutate-multiple-keys-from-regex
|
|
163
|
+
const { cache, mutate } = (0, swr_1.useSWRConfig)();
|
|
164
|
+
return (data, opts) => {
|
|
165
|
+
if (!(cache instanceof Map)) {
|
|
166
|
+
throw new Error('mutate requires the cache provider to be a Map instance');
|
|
167
|
+
}
|
|
168
|
+
const keys = Array.from(cache.keys()).filter((k) => typeof k === 'string' && prefixes.some((prefix) => k.startsWith(prefix)));
|
|
169
|
+
const mutations = keys.map((key) => mutate(key, data, opts));
|
|
170
|
+
return Promise.all(mutations);
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
exports.getMutate = getMutate;
|
|
174
|
+
function fetcher(url, options, fetch, checkReadBack) {
|
|
175
|
+
var _a, _b, _c;
|
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
+
const _fetch = fetch !== null && fetch !== void 0 ? fetch : window.fetch;
|
|
178
|
+
const res = yield _fetch(url, options);
|
|
179
|
+
if (!res.ok) {
|
|
180
|
+
const errData = unmarshal(yield res.text());
|
|
181
|
+
if (checkReadBack !== false &&
|
|
182
|
+
((_a = errData.error) === null || _a === void 0 ? void 0 : _a.prisma) &&
|
|
183
|
+
((_b = errData.error) === null || _b === void 0 ? void 0 : _b.code) === 'P2004' &&
|
|
184
|
+
((_c = errData.error) === null || _c === void 0 ? void 0 : _c.reason) === 'RESULT_NOT_READABLE') {
|
|
185
|
+
// policy doesn't allow mutation result to be read back, just return undefined
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
const error = new Error('An error occurred while fetching the data.');
|
|
189
|
+
error.info = errData.error;
|
|
190
|
+
error.status = res.status;
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
const textResult = yield res.text();
|
|
194
|
+
try {
|
|
195
|
+
return unmarshal(textResult).data;
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
console.error(`Unable to deserialize data:`, textResult);
|
|
199
|
+
throw err;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
exports.fetcher = fetcher;
|
|
204
|
+
function marshal(value) {
|
|
205
|
+
const { data, meta } = (0, browser_1.serialize)(value);
|
|
206
|
+
if (meta) {
|
|
207
|
+
return JSON.stringify(Object.assign(Object.assign({}, data), { meta: { serialization: meta } }));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
return JSON.stringify(data);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function unmarshal(value) {
|
|
214
|
+
var _a;
|
|
215
|
+
const parsed = JSON.parse(value);
|
|
216
|
+
if (parsed.data && ((_a = parsed.meta) === null || _a === void 0 ? void 0 : _a.serialization)) {
|
|
217
|
+
const deserializedData = (0, browser_1.deserialize)(parsed.data, parsed.meta.serialization);
|
|
218
|
+
return Object.assign(Object.assign({}, parsed), { data: deserializedData });
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
return parsed;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function makeUrl(url, args) {
|
|
225
|
+
if (!args) {
|
|
226
|
+
return url;
|
|
227
|
+
}
|
|
228
|
+
const { data, meta } = (0, browser_1.serialize)(args);
|
|
229
|
+
let result = `${url}?q=${encodeURIComponent(JSON.stringify(data))}`;
|
|
230
|
+
if (meta) {
|
|
231
|
+
result += `&meta=${encodeURIComponent(JSON.stringify({ serialization: meta }))}`;
|
|
232
|
+
}
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uDAAuD;AACvD,yDAAqE;AACrE,iCAAsC;AAEtC,2CAA2C;AAC3C,4DAAiH;AACjH,iDAA+B;AAsB/B;;GAEG;AACU,QAAA,qBAAqB,GAAG,IAAA,qBAAa,EAAwB;IACtE,QAAQ,EAAE,YAAY;IACtB,KAAK,EAAE,SAAS;CACnB,CAAC,CAAC;AAEH;;GAEG;AACU,QAAA,QAAQ,GAAG,6BAAqB,CAAC,QAAQ,CAAC;AAgCvD;;;;;;;;GAQG;AACH,8DAA8D;AAC9D,SAAgB,GAAG,CACf,GAAkB,EAClB,IAAc,EACd,OAAuC,EACvC,KAAe;;IAEf,MAAM,MAAM,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAA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C,aAAa,CAAC,CAAC;QAC7E,uCAAY,MAAM,KAAE,IAAI,EAAE,gBAAgB,IAAG;KAChD;SAAM;QACH,OAAO,MAAM,CAAC;KACjB;AACL,CAAC;AAED,SAAS,OAAO,CAAC,GAAW,EAAE,IAAa;IACvC,IAAI,CAAC,IAAI,EAAE;QACP,OAAO,GAAG,CAAC;KACd;IAED,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,IAAA,mBAAS,EAAC,IAAI,CAAC,CAAC;IACvC,IAAI,MAAM,GAAG,GAAG,GAAG,MAAM,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;IACpE,IAAI,IAAI,EAAE;QACN,MAAM,IAAI,SAAS,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;KACpF;IACD,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type Enumerable<T> = T | Array<T>;
|
|
2
|
+
type _TupleToUnion<T> = T extends (infer E)[] ? E : never;
|
|
3
|
+
export type TupleToUnion<K extends readonly any[]> = _TupleToUnion<K>;
|
|
4
|
+
export type MaybeTupleToUnion<T> = T extends any[] ? TupleToUnion<T> : T;
|
|
5
|
+
export type PickEnumerable<T, K extends Enumerable<keyof T> | keyof T> = Pick<T, MaybeTupleToUnion<K>>;
|
|
6
|
+
type SelectAndInclude = {
|
|
7
|
+
select: any;
|
|
8
|
+
include: any;
|
|
9
|
+
};
|
|
10
|
+
type HasSelect = {
|
|
11
|
+
select: any;
|
|
12
|
+
};
|
|
13
|
+
type HasInclude = {
|
|
14
|
+
include: any;
|
|
15
|
+
};
|
|
16
|
+
export type CheckSelect<T, S, U> = T extends SelectAndInclude ? 'Please either choose `select` or `include`' : T extends HasSelect ? U : T extends HasInclude ? U : S;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
/// Types copied over from Prisma's generated code to avoid being broken due to Prisma upgrades
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
//# sourceMappingURL=prisma-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prisma-types.js","sourceRoot":"","sources":["../../src/runtime/prisma-types.ts"],"names":[],"mappings":";AAAA,uDAAuD;AACvD,+FAA+F"}
|
package/res/helper.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
|
|
3
|
-
import { createContext } from 'react';
|
|
4
|
-
import type { MutatorCallback, MutatorOptions, SWRResponse } from 'swr';
|
|
5
|
-
import useSWR, { useSWRConfig } from 'swr';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Function signature for `fetch`.
|
|
9
|
-
*/
|
|
10
|
-
export type FetchFn = (url: string, options?: RequestInit) => Promise<Response>;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Context type for configuring react hooks.
|
|
14
|
-
*/
|
|
15
|
-
export type RequestHandlerContext = {
|
|
16
|
-
/**
|
|
17
|
-
* The endpoint to use for the queries.
|
|
18
|
-
*/
|
|
19
|
-
endpoint: string;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* A custom fetch function for sending the HTTP requests.
|
|
23
|
-
*/
|
|
24
|
-
fetch?: FetchFn;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Context for configuring react hooks.
|
|
29
|
-
*/
|
|
30
|
-
export const RequestHandlerContext = createContext<RequestHandlerContext>({
|
|
31
|
-
endpoint: '/api/model',
|
|
32
|
-
fetch: undefined,
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Context provider.
|
|
37
|
-
*/
|
|
38
|
-
export const Provider = RequestHandlerContext.Provider;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Client request options
|
|
42
|
-
*/
|
|
43
|
-
export type RequestOptions<T> = {
|
|
44
|
-
// disable data fetching
|
|
45
|
-
disabled?: boolean;
|
|
46
|
-
initialData?: T;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Makes a GET request with SWR.
|
|
51
|
-
*
|
|
52
|
-
* @param url The request URL.
|
|
53
|
-
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
|
|
54
|
-
* @returns SWR response
|
|
55
|
-
*/
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
|
-
export function get<Result, Error = any>(
|
|
58
|
-
url: string | null,
|
|
59
|
-
args?: unknown,
|
|
60
|
-
options?: RequestOptions<Result>,
|
|
61
|
-
fetch?: FetchFn
|
|
62
|
-
): SWRResponse<Result, Error> {
|
|
63
|
-
const reqUrl = options?.disabled ? null : url ? makeUrl(url, args) : null;
|
|
64
|
-
return useSWR<Result, Error>(reqUrl, (url) => fetcher(url, undefined, fetch), {
|
|
65
|
-
fallbackData: options?.initialData,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Makes a POST request.
|
|
71
|
-
*
|
|
72
|
-
* @param url The request URL.
|
|
73
|
-
* @param data The request data.
|
|
74
|
-
* @param mutate Mutator for invalidating cache.
|
|
75
|
-
*/
|
|
76
|
-
export async function post<Result>(url: string, data: unknown, mutate: Mutator, fetch?: FetchFn): Promise<Result> {
|
|
77
|
-
const r: Result = await fetcher(
|
|
78
|
-
url,
|
|
79
|
-
{
|
|
80
|
-
method: 'POST',
|
|
81
|
-
headers: {
|
|
82
|
-
'content-type': 'application/json',
|
|
83
|
-
},
|
|
84
|
-
body: marshal(data),
|
|
85
|
-
},
|
|
86
|
-
fetch
|
|
87
|
-
);
|
|
88
|
-
mutate();
|
|
89
|
-
return r;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Makes a PUT request.
|
|
94
|
-
*
|
|
95
|
-
* @param url The request URL.
|
|
96
|
-
* @param data The request data.
|
|
97
|
-
* @param mutate Mutator for invalidating cache.
|
|
98
|
-
*/
|
|
99
|
-
export async function put<Result>(url: string, data: unknown, mutate: Mutator, fetch?: FetchFn): Promise<Result> {
|
|
100
|
-
const r: Result = await fetcher(
|
|
101
|
-
url,
|
|
102
|
-
{
|
|
103
|
-
method: 'PUT',
|
|
104
|
-
headers: {
|
|
105
|
-
'content-type': 'application/json',
|
|
106
|
-
},
|
|
107
|
-
body: marshal(data),
|
|
108
|
-
},
|
|
109
|
-
fetch
|
|
110
|
-
);
|
|
111
|
-
mutate();
|
|
112
|
-
return r;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Makes a DELETE request.
|
|
117
|
-
*
|
|
118
|
-
* @param url The request URL.
|
|
119
|
-
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
|
|
120
|
-
* @param mutate Mutator for invalidating cache.
|
|
121
|
-
*/
|
|
122
|
-
export async function del<Result>(url: string, args: unknown, mutate: Mutator, fetch?: FetchFn): Promise<Result> {
|
|
123
|
-
const reqUrl = makeUrl(url, args);
|
|
124
|
-
const r: Result = await fetcher(
|
|
125
|
-
reqUrl,
|
|
126
|
-
{
|
|
127
|
-
method: 'DELETE',
|
|
128
|
-
},
|
|
129
|
-
fetch
|
|
130
|
-
);
|
|
131
|
-
const path = url.split('/');
|
|
132
|
-
path.pop();
|
|
133
|
-
mutate();
|
|
134
|
-
return r;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
type Mutator = (
|
|
138
|
-
data?: unknown | Promise<unknown> | MutatorCallback,
|
|
139
|
-
opts?: boolean | MutatorOptions
|
|
140
|
-
) => Promise<unknown[]>;
|
|
141
|
-
|
|
142
|
-
export function getMutate(prefixes: string[]): Mutator {
|
|
143
|
-
// https://swr.vercel.app/docs/advanced/cache#mutate-multiple-keys-from-regex
|
|
144
|
-
const { cache, mutate } = useSWRConfig();
|
|
145
|
-
return (data?: unknown | Promise<unknown> | MutatorCallback, opts?: boolean | MutatorOptions) => {
|
|
146
|
-
if (!(cache instanceof Map)) {
|
|
147
|
-
throw new Error('mutate requires the cache provider to be a Map instance');
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const keys = Array.from(cache.keys()).filter(
|
|
151
|
-
(k) => typeof k === 'string' && prefixes.some((prefix) => k.startsWith(prefix))
|
|
152
|
-
) as string[];
|
|
153
|
-
const mutations = keys.map((key) => mutate(key, data, opts));
|
|
154
|
-
return Promise.all(mutations);
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export async function fetcher<R>(url: string, options?: RequestInit, fetch?: FetchFn) {
|
|
159
|
-
const _fetch = fetch ?? window.fetch;
|
|
160
|
-
const res = await _fetch(url, options);
|
|
161
|
-
if (!res.ok) {
|
|
162
|
-
const error: Error & { info?: unknown; status?: number } = new Error(
|
|
163
|
-
'An error occurred while fetching the data.'
|
|
164
|
-
);
|
|
165
|
-
error.info = unmarshal(await res.text());
|
|
166
|
-
error.status = res.status;
|
|
167
|
-
throw error;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const textResult = await res.text();
|
|
171
|
-
try {
|
|
172
|
-
return unmarshal(textResult) as R;
|
|
173
|
-
} catch (err) {
|
|
174
|
-
console.error(`Unable to deserialize data:`, textResult);
|
|
175
|
-
throw err;
|
|
176
|
-
}
|
|
177
|
-
}
|
package/res/marshal-json.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
function marshal(value: unknown) {
|
|
2
|
-
return JSON.stringify(value);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
function unmarshal(value: string) {
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
-
return JSON.parse(value) as any;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function makeUrl(url: string, args: unknown) {
|
|
11
|
-
return args ? url + `?q=${encodeURIComponent(JSON.stringify(args))}` : url;
|
|
12
|
-
}
|
package/res/marshal-superjson.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import superjson from 'superjson';
|
|
2
|
-
|
|
3
|
-
function marshal(value: unknown) {
|
|
4
|
-
return superjson.stringify(value);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function unmarshal(value: string) {
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
-
const j = JSON.parse(value) as any;
|
|
10
|
-
if (j?.json) {
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
-
return superjson.parse<any>(value);
|
|
13
|
-
} else {
|
|
14
|
-
return j;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function makeUrl(url: string, args: unknown) {
|
|
19
|
-
return args ? url + `?q=${encodeURIComponent(superjson.stringify(args))}` : url;
|
|
20
|
-
}
|