prisma-swagger-autogen 1.0.7 → 1.0.8

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/cli.js CHANGED
@@ -1,9 +1,198 @@
1
- import {
2
- run
3
- } from "./chunk-HDG7TBSN.js";
1
+ // src/index.ts
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import { createRequire } from "module";
5
+ import { globSync } from "glob";
6
+ var CONFIG = {
7
+ projectRoot: process.cwd(),
8
+ controllersGlob: "./src/web/api/controllers/**/*.ts",
9
+ outFile: "./swagger.config.js",
10
+ openapiOut: "./src/web/api/openapi.json",
11
+ serviceTitle: "Prescription Service",
12
+ serverUrl: "http://localhost:3008",
13
+ securitySchemeName: "keycloakOAuth",
14
+ oauth: {
15
+ tokenUrl: "http://auth.localhost/realms/haemo/protocol/openid-connect/token",
16
+ refreshUrl: "http://auth.localhost/realms/haemo/protocol/openid-connect/refresh",
17
+ scopes: { openid: "openid scope" }
18
+ },
19
+ omitFieldsInWriteDtos: /* @__PURE__ */ new Set(["id", "createdAt", "updatedAt", "v"])
20
+ };
21
+ function ensurePosix(p) {
22
+ return p.split(path.sep).join(path.posix.sep);
23
+ }
24
+ function pluralize(name) {
25
+ if (name.endsWith("s")) return `${name}es`;
26
+ return `${name}s`;
27
+ }
28
+ function getRequire() {
29
+ return createRequire(typeof __filename !== "undefined" ? __filename : process.cwd() + "/");
30
+ }
31
+ async function loadDmmfFromProject(schemaPath) {
32
+ const resolvedSchemaPath = schemaPath ? path.resolve(process.cwd(), schemaPath) : path.resolve(process.cwd(), "prisma/schema.prisma");
33
+ if (!fs.existsSync(resolvedSchemaPath)) {
34
+ throw new Error(`Prisma schema not found at ${resolvedSchemaPath}`);
35
+ }
36
+ const datamodel = fs.readFileSync(resolvedSchemaPath, "utf8");
37
+ const require2 = getRequire();
38
+ const internals = require2("@prisma/internals");
39
+ if (typeof internals.getDMMF !== "function") {
40
+ throw new Error(`@prisma/internals.getDMMF not available`);
41
+ }
42
+ const dmmf = await internals.getDMMF({ datamodel });
43
+ if (!dmmf || !dmmf.datamodel || !Array.isArray(dmmf.datamodel.models) || !Array.isArray(dmmf.datamodel.enums)) {
44
+ throw new Error(`Unexpected DMMF shape returned by @prisma/internals.getDMMF`);
45
+ }
46
+ return dmmf;
47
+ }
48
+ function scalarToSchema(scalar) {
49
+ switch (scalar) {
50
+ case "String":
51
+ return { type: "string" };
52
+ case "Boolean":
53
+ return { type: "boolean" };
54
+ case "Int":
55
+ return { type: "integer" };
56
+ case "BigInt":
57
+ return { type: "integer", format: "int64" };
58
+ case "Float":
59
+ return { type: "number" };
60
+ case "Decimal":
61
+ return { type: "number" };
62
+ case "DateTime":
63
+ return { type: "string", format: "date-time" };
64
+ case "Json":
65
+ return { type: "object" };
66
+ case "Bytes":
67
+ return { type: "string", format: "byte" };
68
+ default:
69
+ return { type: "string" };
70
+ }
71
+ }
72
+ function fieldSchema(field, getRefName) {
73
+ if (field.kind === "scalar") {
74
+ const base = scalarToSchema(field.type);
75
+ if (field.isList) return { type: "array", items: base };
76
+ return base;
77
+ }
78
+ if (field.kind === "enum") {
79
+ const base = { $ref: `#/components/schemas/${field.type}` };
80
+ if (field.isList) return { type: "array", items: base };
81
+ return base;
82
+ }
83
+ if (field.kind === "object") {
84
+ const ref = { $ref: `#/components/schemas/${getRefName(String(field.type))}` };
85
+ if (field.isList) return { type: "array", items: ref };
86
+ return ref;
87
+ }
88
+ return { type: "object" };
89
+ }
90
+ function modelToGetSchema(model, getRefName) {
91
+ const properties = {};
92
+ const required = [];
93
+ for (const f of model.fields) {
94
+ properties[f.name] = fieldSchema(f, getRefName);
95
+ if (f.isRequired) required.push(f.name);
96
+ }
97
+ const schema = { type: "object", properties };
98
+ if (required.length) schema.required = required;
99
+ return schema;
100
+ }
101
+ function stripWriteFields(model, getSchema, omit) {
102
+ const schema = JSON.parse(JSON.stringify(getSchema));
103
+ if (!schema.properties) return schema;
104
+ const relationFieldNames = new Set(model.fields.filter((f) => f.kind === "object").map((f) => f.name));
105
+ for (const key of Object.keys(schema.properties)) {
106
+ if (omit.has(key) || relationFieldNames.has(key)) delete schema.properties[key];
107
+ }
108
+ if (Array.isArray(schema.required)) {
109
+ schema.required = schema.required.filter((k) => !omit.has(k) && !relationFieldNames.has(k));
110
+ if (schema.required.length === 0) delete schema.required;
111
+ }
112
+ return schema;
113
+ }
114
+ function makeAllOptional(schema) {
115
+ const s = JSON.parse(JSON.stringify(schema));
116
+ delete s.required;
117
+ return s;
118
+ }
119
+ function listResponseSchema(itemRef) {
120
+ return {
121
+ type: "object",
122
+ properties: {
123
+ count: { type: "number" },
124
+ hasPreviousPage: { type: "boolean" },
125
+ hasNextPage: { type: "boolean" },
126
+ pageNumber: { type: "number" },
127
+ pageSize: { type: "number" },
128
+ totalPages: { type: "number" },
129
+ items: { type: "array", items: { $ref: itemRef } }
130
+ },
131
+ required: ["count", "hasPreviousPage", "hasNextPage", "pageNumber", "pageSize", "totalPages", "items"]
132
+ };
133
+ }
134
+ async function buildSchemasFromPrismaDmmf(schemaPath) {
135
+ const dmmf = await loadDmmfFromProject(schemaPath);
136
+ const schemas = {};
137
+ const getRefName = (modelName) => `Get${modelName}Response`;
138
+ for (const e of dmmf.datamodel.enums) {
139
+ schemas[e.name] = { type: "string", enum: e.values.map((v) => v.name) };
140
+ }
141
+ for (const model of dmmf.datamodel.models) {
142
+ const getName = `Get${model.name}Response`;
143
+ const postName = `Post${model.name}Request`;
144
+ const putName = `Put${model.name}Request`;
145
+ const listName = `List${pluralize(model.name)}Response`;
146
+ const getSchema = modelToGetSchema(model, getRefName);
147
+ const postSchema = stripWriteFields(model, getSchema, CONFIG.omitFieldsInWriteDtos);
148
+ const putSchema = makeAllOptional(postSchema);
149
+ schemas[getName] = getSchema;
150
+ schemas[postName] = postSchema;
151
+ schemas[putName] = putSchema;
152
+ schemas[listName] = listResponseSchema(`#/components/schemas/${getName}`);
153
+ }
154
+ return schemas;
155
+ }
156
+ function generateSwaggerConfigJs(schemas) {
157
+ const routes = globSync(CONFIG.controllersGlob, { nodir: true }).map((p) => ensurePosix(p));
158
+ const docs = {
159
+ info: { title: CONFIG.serviceTitle },
160
+ servers: [{ url: CONFIG.serverUrl }],
161
+ components: {
162
+ schemas,
163
+ securitySchemes: {
164
+ [CONFIG.securitySchemeName]: {
165
+ type: "oauth2",
166
+ description: "This API uses OAuth2 with the password flow.",
167
+ flows: {
168
+ password: {
169
+ tokenUrl: CONFIG.oauth.tokenUrl,
170
+ refreshUrl: CONFIG.oauth.refreshUrl,
171
+ scopes: CONFIG.oauth.scopes
172
+ }
173
+ }
174
+ }
175
+ }
176
+ },
177
+ security: [{ [CONFIG.securitySchemeName]: ["openid"] }]
178
+ };
179
+ const fileContent = `const swaggerAutogen = require('swagger-autogen')();
180
+ const docs = ${JSON.stringify(docs, null, 2)};
181
+ const routes = ${JSON.stringify(routes, null, 2)};
182
+ swaggerAutogen('${ensurePosix(CONFIG.openapiOut)}', routes, docs);`;
183
+ fs.writeFileSync(path.resolve(CONFIG.projectRoot, CONFIG.outFile), fileContent, "utf8");
184
+ }
185
+ async function run(args = []) {
186
+ const schemaFlagIndex = args.findIndex((a) => a === "--schema");
187
+ const schemaPath = schemaFlagIndex >= 0 ? args[schemaFlagIndex + 1] : void 0;
188
+ const schemas = await buildSchemasFromPrismaDmmf(schemaPath);
189
+ generateSwaggerConfigJs(schemas);
190
+ }
4
191
 
5
192
  // src/cli.ts
6
193
  void run(process.argv.slice(2)).catch((e) => {
7
194
  console.error(e);
8
195
  process.exitCode = 1;
9
196
  });
197
+ ss.exitCode = 1;
198
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-swagger-autogen",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Generate swagger-autogen config + Prisma DMMF schemas and fix swagger-autogen output.",
5
5
  "license": "MIT",
6
6
  "author": "Joyce Marvin Rafflenbeul",
@@ -39,7 +39,7 @@
39
39
  "LICENSE"
40
40
  ],
41
41
  "scripts": {
42
- "build": "tsup src/index.ts src/cli.ts --format esm,cjs --dts --out-dir dist --tsconfig tsconfig.json && node scripts/make-bin.cjs && chmod +x dist/bin.cjs",
42
+ "build": "tsup src/index.ts --format esm,cjs --dts --out-dir dist && tsup src/cli.ts --format esm --out-dir dist --no-dts",
43
43
  "prepublishOnly": "npm run build"
44
44
  },
45
45
  "dependencies": {
package/dist/bin.cjs DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- require('./cli.cjs');
@@ -1,197 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/index.ts
2
- var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
3
- var _path = require('path'); var _path2 = _interopRequireDefault(_path);
4
- var _module = require('module');
5
- var _glob = require('glob');
6
- var CONFIG = {
7
- projectRoot: process.cwd(),
8
- controllersGlob: "./src/web/api/controllers/**/*.ts",
9
- outFile: "./swagger.config.js",
10
- openapiOut: "./src/web/api/openapi.json",
11
- serviceTitle: "Prescription Service",
12
- serverUrl: "http://localhost:3008",
13
- securitySchemeName: "keycloakOAuth",
14
- oauth: {
15
- tokenUrl: "http://auth.localhost/realms/haemo/protocol/openid-connect/token",
16
- refreshUrl: "http://auth.localhost/realms/haemo/protocol/openid-connect/refresh",
17
- scopes: { openid: "openid scope" }
18
- },
19
- omitFieldsInWriteDtos: /* @__PURE__ */ new Set(["id", "createdAt", "updatedAt", "v"])
20
- };
21
- function ensurePosix(p) {
22
- return p.split(_path2.default.sep).join(_path2.default.posix.sep);
23
- }
24
- function pluralize(name) {
25
- if (name.endsWith("s")) return `${name}es`;
26
- return `${name}s`;
27
- }
28
- function getRequire() {
29
- const base = typeof __filename !== "undefined" ? __filename : import.meta.url;
30
- return _module.createRequire.call(void 0, base);
31
- }
32
- async function loadDmmfFromProject(schemaPath) {
33
- const resolvedSchemaPath = schemaPath ? _path2.default.resolve(process.cwd(), schemaPath) : _path2.default.resolve(process.cwd(), "prisma/schema.prisma");
34
- if (!_fs2.default.existsSync(resolvedSchemaPath)) {
35
- throw new Error(`Prisma schema not found at ${resolvedSchemaPath}`);
36
- }
37
- const datamodel = _fs2.default.readFileSync(resolvedSchemaPath, "utf8");
38
- const require2 = getRequire();
39
- const internals = require2("@prisma/internals");
40
- if (typeof internals.getDMMF !== "function") {
41
- throw new Error(`@prisma/internals.getDMMF not available`);
42
- }
43
- const dmmf = await internals.getDMMF({ datamodel });
44
- if (!dmmf || !dmmf.datamodel || !Array.isArray(dmmf.datamodel.models) || !Array.isArray(dmmf.datamodel.enums)) {
45
- throw new Error(`Unexpected DMMF shape returned by @prisma/internals.getDMMF`);
46
- }
47
- return dmmf;
48
- }
49
- function scalarToSchema(scalar) {
50
- switch (scalar) {
51
- case "String":
52
- return { type: "string" };
53
- case "Boolean":
54
- return { type: "boolean" };
55
- case "Int":
56
- return { type: "integer" };
57
- case "BigInt":
58
- return { type: "integer", format: "int64" };
59
- case "Float":
60
- return { type: "number" };
61
- case "Decimal":
62
- return { type: "number" };
63
- case "DateTime":
64
- return { type: "string", format: "date-time" };
65
- case "Json":
66
- return { type: "object" };
67
- case "Bytes":
68
- return { type: "string", format: "byte" };
69
- default:
70
- return { type: "string" };
71
- }
72
- }
73
- function fieldSchema(field, getRefName) {
74
- if (field.kind === "scalar") {
75
- const base = scalarToSchema(field.type);
76
- if (field.isList) return { type: "array", items: base };
77
- return base;
78
- }
79
- if (field.kind === "enum") {
80
- const base = { $ref: `#/components/schemas/${field.type}` };
81
- if (field.isList) return { type: "array", items: base };
82
- return base;
83
- }
84
- if (field.kind === "object") {
85
- const ref = { $ref: `#/components/schemas/${getRefName(String(field.type))}` };
86
- if (field.isList) return { type: "array", items: ref };
87
- return ref;
88
- }
89
- return { type: "object" };
90
- }
91
- function modelToGetSchema(model, getRefName) {
92
- const properties = {};
93
- const required = [];
94
- for (const f of model.fields) {
95
- properties[f.name] = fieldSchema(f, getRefName);
96
- if (f.isRequired) required.push(f.name);
97
- }
98
- const schema = { type: "object", properties };
99
- if (required.length) schema.required = required;
100
- return schema;
101
- }
102
- function stripWriteFields(model, getSchema, omit) {
103
- const schema = JSON.parse(JSON.stringify(getSchema));
104
- if (!schema.properties) return schema;
105
- const relationFieldNames = new Set(model.fields.filter((f) => f.kind === "object").map((f) => f.name));
106
- for (const key of Object.keys(schema.properties)) {
107
- if (omit.has(key) || relationFieldNames.has(key)) {
108
- delete schema.properties[key];
109
- }
110
- }
111
- if (Array.isArray(schema.required)) {
112
- schema.required = schema.required.filter((k) => !omit.has(k) && !relationFieldNames.has(k));
113
- if (schema.required.length === 0) delete schema.required;
114
- }
115
- return schema;
116
- }
117
- function makeAllOptional(schema) {
118
- const s = JSON.parse(JSON.stringify(schema));
119
- delete s.required;
120
- return s;
121
- }
122
- function listResponseSchema(itemRef) {
123
- return {
124
- type: "object",
125
- properties: {
126
- count: { type: "number" },
127
- hasPreviousPage: { type: "boolean" },
128
- hasNextPage: { type: "boolean" },
129
- pageNumber: { type: "number" },
130
- pageSize: { type: "number" },
131
- totalPages: { type: "number" },
132
- items: { type: "array", items: { $ref: itemRef } }
133
- },
134
- required: ["count", "hasPreviousPage", "hasNextPage", "pageNumber", "pageSize", "totalPages", "items"]
135
- };
136
- }
137
- async function buildSchemasFromPrismaDmmf(schemaPath) {
138
- const dmmf = await loadDmmfFromProject(schemaPath);
139
- const schemas = {};
140
- const getRefName = (modelName) => `Get${modelName}Response`;
141
- for (const e of dmmf.datamodel.enums) {
142
- schemas[e.name] = { type: "string", enum: e.values.map((v) => v.name) };
143
- }
144
- for (const model of dmmf.datamodel.models) {
145
- const getName = `Get${model.name}Response`;
146
- const postName = `Post${model.name}Request`;
147
- const putName = `Put${model.name}Request`;
148
- const listName = `List${pluralize(model.name)}Response`;
149
- const getSchema = modelToGetSchema(model, getRefName);
150
- const postSchema = stripWriteFields(model, getSchema, CONFIG.omitFieldsInWriteDtos);
151
- const putSchema = makeAllOptional(postSchema);
152
- schemas[getName] = getSchema;
153
- schemas[postName] = postSchema;
154
- schemas[putName] = putSchema;
155
- schemas[listName] = listResponseSchema(`#/components/schemas/${getName}`);
156
- }
157
- return schemas;
158
- }
159
- function generateSwaggerConfigJs(schemas) {
160
- const routes = _glob.globSync.call(void 0, CONFIG.controllersGlob, { nodir: true }).map((p) => ensurePosix(p));
161
- const docs = {
162
- info: { title: CONFIG.serviceTitle },
163
- servers: [{ url: CONFIG.serverUrl }],
164
- components: {
165
- schemas,
166
- securitySchemes: {
167
- [CONFIG.securitySchemeName]: {
168
- type: "oauth2",
169
- description: "This API uses OAuth2 with the password flow.",
170
- flows: {
171
- password: {
172
- tokenUrl: CONFIG.oauth.tokenUrl,
173
- refreshUrl: CONFIG.oauth.refreshUrl,
174
- scopes: CONFIG.oauth.scopes
175
- }
176
- }
177
- }
178
- }
179
- },
180
- security: [{ [CONFIG.securitySchemeName]: ["openid"] }]
181
- };
182
- const fileContent = `const swaggerAutogen = require('swagger-autogen')();
183
- const docs = ${JSON.stringify(docs, null, 2)};
184
- const routes = ${JSON.stringify(routes, null, 2)};
185
- swaggerAutogen('${ensurePosix(CONFIG.openapiOut)}', routes, docs);`;
186
- _fs2.default.writeFileSync(_path2.default.resolve(CONFIG.projectRoot, CONFIG.outFile), fileContent, "utf8");
187
- }
188
- async function run(args = []) {
189
- const schemaFlagIndex = args.findIndex((a) => a === "--schema");
190
- const schemaPath = schemaFlagIndex >= 0 ? args[schemaFlagIndex + 1] : void 0;
191
- const schemas = await buildSchemasFromPrismaDmmf(schemaPath);
192
- generateSwaggerConfigJs(schemas);
193
- }
194
-
195
-
196
-
197
- exports.run = run;
@@ -1,197 +0,0 @@
1
- // src/index.ts
2
- import fs from "fs";
3
- import path from "path";
4
- import { createRequire } from "module";
5
- import { globSync } from "glob";
6
- var CONFIG = {
7
- projectRoot: process.cwd(),
8
- controllersGlob: "./src/web/api/controllers/**/*.ts",
9
- outFile: "./swagger.config.js",
10
- openapiOut: "./src/web/api/openapi.json",
11
- serviceTitle: "Prescription Service",
12
- serverUrl: "http://localhost:3008",
13
- securitySchemeName: "keycloakOAuth",
14
- oauth: {
15
- tokenUrl: "http://auth.localhost/realms/haemo/protocol/openid-connect/token",
16
- refreshUrl: "http://auth.localhost/realms/haemo/protocol/openid-connect/refresh",
17
- scopes: { openid: "openid scope" }
18
- },
19
- omitFieldsInWriteDtos: /* @__PURE__ */ new Set(["id", "createdAt", "updatedAt", "v"])
20
- };
21
- function ensurePosix(p) {
22
- return p.split(path.sep).join(path.posix.sep);
23
- }
24
- function pluralize(name) {
25
- if (name.endsWith("s")) return `${name}es`;
26
- return `${name}s`;
27
- }
28
- function getRequire() {
29
- const base = typeof __filename !== "undefined" ? __filename : import.meta.url;
30
- return createRequire(base);
31
- }
32
- async function loadDmmfFromProject(schemaPath) {
33
- const resolvedSchemaPath = schemaPath ? path.resolve(process.cwd(), schemaPath) : path.resolve(process.cwd(), "prisma/schema.prisma");
34
- if (!fs.existsSync(resolvedSchemaPath)) {
35
- throw new Error(`Prisma schema not found at ${resolvedSchemaPath}`);
36
- }
37
- const datamodel = fs.readFileSync(resolvedSchemaPath, "utf8");
38
- const require2 = getRequire();
39
- const internals = require2("@prisma/internals");
40
- if (typeof internals.getDMMF !== "function") {
41
- throw new Error(`@prisma/internals.getDMMF not available`);
42
- }
43
- const dmmf = await internals.getDMMF({ datamodel });
44
- if (!dmmf || !dmmf.datamodel || !Array.isArray(dmmf.datamodel.models) || !Array.isArray(dmmf.datamodel.enums)) {
45
- throw new Error(`Unexpected DMMF shape returned by @prisma/internals.getDMMF`);
46
- }
47
- return dmmf;
48
- }
49
- function scalarToSchema(scalar) {
50
- switch (scalar) {
51
- case "String":
52
- return { type: "string" };
53
- case "Boolean":
54
- return { type: "boolean" };
55
- case "Int":
56
- return { type: "integer" };
57
- case "BigInt":
58
- return { type: "integer", format: "int64" };
59
- case "Float":
60
- return { type: "number" };
61
- case "Decimal":
62
- return { type: "number" };
63
- case "DateTime":
64
- return { type: "string", format: "date-time" };
65
- case "Json":
66
- return { type: "object" };
67
- case "Bytes":
68
- return { type: "string", format: "byte" };
69
- default:
70
- return { type: "string" };
71
- }
72
- }
73
- function fieldSchema(field, getRefName) {
74
- if (field.kind === "scalar") {
75
- const base = scalarToSchema(field.type);
76
- if (field.isList) return { type: "array", items: base };
77
- return base;
78
- }
79
- if (field.kind === "enum") {
80
- const base = { $ref: `#/components/schemas/${field.type}` };
81
- if (field.isList) return { type: "array", items: base };
82
- return base;
83
- }
84
- if (field.kind === "object") {
85
- const ref = { $ref: `#/components/schemas/${getRefName(String(field.type))}` };
86
- if (field.isList) return { type: "array", items: ref };
87
- return ref;
88
- }
89
- return { type: "object" };
90
- }
91
- function modelToGetSchema(model, getRefName) {
92
- const properties = {};
93
- const required = [];
94
- for (const f of model.fields) {
95
- properties[f.name] = fieldSchema(f, getRefName);
96
- if (f.isRequired) required.push(f.name);
97
- }
98
- const schema = { type: "object", properties };
99
- if (required.length) schema.required = required;
100
- return schema;
101
- }
102
- function stripWriteFields(model, getSchema, omit) {
103
- const schema = JSON.parse(JSON.stringify(getSchema));
104
- if (!schema.properties) return schema;
105
- const relationFieldNames = new Set(model.fields.filter((f) => f.kind === "object").map((f) => f.name));
106
- for (const key of Object.keys(schema.properties)) {
107
- if (omit.has(key) || relationFieldNames.has(key)) {
108
- delete schema.properties[key];
109
- }
110
- }
111
- if (Array.isArray(schema.required)) {
112
- schema.required = schema.required.filter((k) => !omit.has(k) && !relationFieldNames.has(k));
113
- if (schema.required.length === 0) delete schema.required;
114
- }
115
- return schema;
116
- }
117
- function makeAllOptional(schema) {
118
- const s = JSON.parse(JSON.stringify(schema));
119
- delete s.required;
120
- return s;
121
- }
122
- function listResponseSchema(itemRef) {
123
- return {
124
- type: "object",
125
- properties: {
126
- count: { type: "number" },
127
- hasPreviousPage: { type: "boolean" },
128
- hasNextPage: { type: "boolean" },
129
- pageNumber: { type: "number" },
130
- pageSize: { type: "number" },
131
- totalPages: { type: "number" },
132
- items: { type: "array", items: { $ref: itemRef } }
133
- },
134
- required: ["count", "hasPreviousPage", "hasNextPage", "pageNumber", "pageSize", "totalPages", "items"]
135
- };
136
- }
137
- async function buildSchemasFromPrismaDmmf(schemaPath) {
138
- const dmmf = await loadDmmfFromProject(schemaPath);
139
- const schemas = {};
140
- const getRefName = (modelName) => `Get${modelName}Response`;
141
- for (const e of dmmf.datamodel.enums) {
142
- schemas[e.name] = { type: "string", enum: e.values.map((v) => v.name) };
143
- }
144
- for (const model of dmmf.datamodel.models) {
145
- const getName = `Get${model.name}Response`;
146
- const postName = `Post${model.name}Request`;
147
- const putName = `Put${model.name}Request`;
148
- const listName = `List${pluralize(model.name)}Response`;
149
- const getSchema = modelToGetSchema(model, getRefName);
150
- const postSchema = stripWriteFields(model, getSchema, CONFIG.omitFieldsInWriteDtos);
151
- const putSchema = makeAllOptional(postSchema);
152
- schemas[getName] = getSchema;
153
- schemas[postName] = postSchema;
154
- schemas[putName] = putSchema;
155
- schemas[listName] = listResponseSchema(`#/components/schemas/${getName}`);
156
- }
157
- return schemas;
158
- }
159
- function generateSwaggerConfigJs(schemas) {
160
- const routes = globSync(CONFIG.controllersGlob, { nodir: true }).map((p) => ensurePosix(p));
161
- const docs = {
162
- info: { title: CONFIG.serviceTitle },
163
- servers: [{ url: CONFIG.serverUrl }],
164
- components: {
165
- schemas,
166
- securitySchemes: {
167
- [CONFIG.securitySchemeName]: {
168
- type: "oauth2",
169
- description: "This API uses OAuth2 with the password flow.",
170
- flows: {
171
- password: {
172
- tokenUrl: CONFIG.oauth.tokenUrl,
173
- refreshUrl: CONFIG.oauth.refreshUrl,
174
- scopes: CONFIG.oauth.scopes
175
- }
176
- }
177
- }
178
- }
179
- },
180
- security: [{ [CONFIG.securitySchemeName]: ["openid"] }]
181
- };
182
- const fileContent = `const swaggerAutogen = require('swagger-autogen')();
183
- const docs = ${JSON.stringify(docs, null, 2)};
184
- const routes = ${JSON.stringify(routes, null, 2)};
185
- swaggerAutogen('${ensurePosix(CONFIG.openapiOut)}', routes, docs);`;
186
- fs.writeFileSync(path.resolve(CONFIG.projectRoot, CONFIG.outFile), fileContent, "utf8");
187
- }
188
- async function run(args = []) {
189
- const schemaFlagIndex = args.findIndex((a) => a === "--schema");
190
- const schemaPath = schemaFlagIndex >= 0 ? args[schemaFlagIndex + 1] : void 0;
191
- const schemas = await buildSchemasFromPrismaDmmf(schemaPath);
192
- generateSwaggerConfigJs(schemas);
193
- }
194
-
195
- export {
196
- run
197
- };
package/dist/cli.cjs DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- var _chunkD67FQWAIcjs = require('./chunk-D67FQWAI.cjs');
4
-
5
- // src/cli.ts
6
- void _chunkD67FQWAIcjs.run.call(void 0, process.argv.slice(2)).catch((e) => {
7
- console.error(e);
8
- process.exitCode = 1;
9
- });
package/dist/cli.d.cts DELETED
@@ -1,2 +0,0 @@
1
-
2
- export { }
package/dist/cli.d.ts DELETED
@@ -1,2 +0,0 @@
1
-
2
- export { }
package/dist/index.cjs DELETED
@@ -1,6 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
- var _chunkD67FQWAIcjs = require('./chunk-D67FQWAI.cjs');
4
-
5
-
6
- exports.run = _chunkD67FQWAIcjs.run;
package/dist/index.d.cts DELETED
@@ -1,3 +0,0 @@
1
- declare function run(args?: string[]): Promise<void>;
2
-
3
- export { run };
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- declare function run(args?: string[]): Promise<void>;
2
-
3
- export { run };
package/dist/index.js DELETED
@@ -1,6 +0,0 @@
1
- import {
2
- run
3
- } from "./chunk-HDG7TBSN.js";
4
- export {
5
- run
6
- };