firebase-contract 0.1.0-rc.3 → 0.1.0-rc.5
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/lib/_internal/generators/api/api-dto-generator.d.ts.map +1 -1
- package/dist/lib/_internal/generators/api/api-dto-generator.js +17 -5
- package/dist/lib/_internal/generators/api/api-validation-generator.d.ts.map +1 -1
- package/dist/lib/_internal/generators/api/api-validation-generator.js +21 -3
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-dto-generator.d.ts","sourceRoot":"","sources":["../../../../../src/lib/_internal/generators/api/api-dto-generator.ts"],"names":[],"mappings":"AAGA,OAAO,EAAiB,SAAS,EAAoB,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"api-dto-generator.d.ts","sourceRoot":"","sources":["../../../../../src/lib/_internal/generators/api/api-dto-generator.ts"],"names":[],"mappings":"AAGA,OAAO,EAAiB,SAAS,EAAoB,MAAM,iBAAiB,CAAA;AA0G5E;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,QAAO,SAkBvC,CAAA"}
|
|
@@ -94,21 +94,31 @@ const decoratorsFor = (ir, field, used) => {
|
|
|
94
94
|
}
|
|
95
95
|
return out;
|
|
96
96
|
};
|
|
97
|
+
const jsdoc = (description, indent) => {
|
|
98
|
+
if (!description)
|
|
99
|
+
return '';
|
|
100
|
+
const lines = description.split('\n');
|
|
101
|
+
if (lines.length === 1)
|
|
102
|
+
return `${indent}/** ${lines[0]} */\n`;
|
|
103
|
+
return `${indent}/**\n${lines.map(line => `${indent} * ${line}`.trimEnd()).join('\n')}\n${indent} */\n`;
|
|
104
|
+
};
|
|
97
105
|
const renderDto = (ir, api, used) => {
|
|
98
106
|
const fields = payloadFields(ir, api.request);
|
|
99
107
|
const className = `${pascalCase(api.name)}Dto`;
|
|
108
|
+
const doc = jsdoc(api.description, '');
|
|
100
109
|
if (api.request.isVoid || fields.length === 0) {
|
|
101
|
-
return
|
|
110
|
+
return `${doc}export class ${className} {}`;
|
|
102
111
|
}
|
|
103
112
|
const members = fields
|
|
104
113
|
.map(field => {
|
|
105
114
|
const name = isRelation(field) ? relationFkName(field) : field.name;
|
|
106
115
|
const decorators = decoratorsFor(ir, field, used).join('\n');
|
|
107
116
|
const mark = field.optional ? '?' : '!';
|
|
108
|
-
|
|
117
|
+
const nullable = field.nullable ? ' | null' : '';
|
|
118
|
+
return `${jsdoc(field.description, ' ')}${decorators}\n ${name}${mark}: ${propType(ir, field)}${nullable}`;
|
|
109
119
|
})
|
|
110
120
|
.join('\n\n');
|
|
111
|
-
return
|
|
121
|
+
return `${doc}export class ${className} {\n${members}\n}`;
|
|
112
122
|
};
|
|
113
123
|
/**
|
|
114
124
|
* Generates class-validator DTO classes for API endpoint requests, mirroring the
|
|
@@ -119,11 +129,13 @@ export const createApiDtoGenerator = () => ({
|
|
|
119
129
|
name: 'api-dto',
|
|
120
130
|
description: 'class-validator DTO classes for API requests',
|
|
121
131
|
generate(ir, context) {
|
|
122
|
-
|
|
132
|
+
// DTO は HTTP リクエストボディ用 — task / pubsub のペイロードは task-payloads が担う。
|
|
133
|
+
const httpApis = ir.apis.filter(api => api.kind === 'https' || api.kind === 'callable');
|
|
134
|
+
if (httpApis.length === 0) {
|
|
123
135
|
return [];
|
|
124
136
|
}
|
|
125
137
|
const used = new Set();
|
|
126
|
-
const dtoBlocks =
|
|
138
|
+
const dtoBlocks = httpApis.map(api => renderDto(ir, api, used));
|
|
127
139
|
const blocks = [...headerBlocks(context)];
|
|
128
140
|
if (used.size > 0) {
|
|
129
141
|
blocks.push(`import { ${[...used].sort().join(', ')} } from 'class-validator'`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-validation-generator.d.ts","sourceRoot":"","sources":["../../../../../src/lib/_internal/generators/api/api-validation-generator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAiB,SAAS,EAAoB,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"api-validation-generator.d.ts","sourceRoot":"","sources":["../../../../../src/lib/_internal/generators/api/api-validation-generator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAiB,SAAS,EAAoB,MAAM,iBAAiB,CAAA;AAuE5E;;;GAGG;AACH,eAAO,MAAM,4BAA4B,QAAO,SAa9C,CAAA"}
|
|
@@ -21,8 +21,7 @@ const payloadFields = (ir, payload) => {
|
|
|
21
21
|
}
|
|
22
22
|
return payload.fields;
|
|
23
23
|
};
|
|
24
|
-
const
|
|
25
|
-
const name = isRelation(field) ? relationFkName(field) : field.name;
|
|
24
|
+
const fieldChain = (ir, field, seen) => {
|
|
26
25
|
let schema;
|
|
27
26
|
if (isRelation(field)) {
|
|
28
27
|
schema = SCALAR_ZOD[relationFkType(ir, field)];
|
|
@@ -35,6 +34,21 @@ const fieldSchema = (ir, field) => {
|
|
|
35
34
|
const irEnum = ir.enums.find(e => e.name === enumName);
|
|
36
35
|
schema = irEnum ? `z.enum([${irEnum.values.map(singleQuote).join(', ')}])` : 'z.string()';
|
|
37
36
|
}
|
|
37
|
+
else if (field.type.kind === 'model') {
|
|
38
|
+
// Embedded value objects expand to a structural z.object so request payloads
|
|
39
|
+
// validate all the way down; cycles fall back to z.unknown().
|
|
40
|
+
const model = findModel(ir, field.type.name);
|
|
41
|
+
if (model && !seen.has(model.name)) {
|
|
42
|
+
const nested = new Set([...seen, model.name]);
|
|
43
|
+
const body = model.fields
|
|
44
|
+
.map(f => `${isRelation(f) ? relationFkName(f) : f.name}: ${fieldChain(ir, f, nested)}`)
|
|
45
|
+
.join(', ');
|
|
46
|
+
schema = `z.object({ ${body} })`;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
schema = 'z.unknown()';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
38
52
|
else {
|
|
39
53
|
schema = 'z.unknown()';
|
|
40
54
|
}
|
|
@@ -44,7 +58,11 @@ const fieldSchema = (ir, field) => {
|
|
|
44
58
|
schema = `${schema}.nullable()`;
|
|
45
59
|
if (field.optional)
|
|
46
60
|
schema = `${schema}.optional()`;
|
|
47
|
-
return
|
|
61
|
+
return schema;
|
|
62
|
+
};
|
|
63
|
+
const fieldSchema = (ir, field) => {
|
|
64
|
+
const name = isRelation(field) ? relationFkName(field) : field.name;
|
|
65
|
+
return ` ${name}: ${fieldChain(ir, field, new Set())},`;
|
|
48
66
|
};
|
|
49
67
|
const renderRequestSchema = (ir, api) => {
|
|
50
68
|
const name = `${pascalCase(api.name)}RequestSchema`;
|