typed-openapi 0.1.1 → 0.1.2

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.cjs CHANGED
@@ -30,8 +30,8 @@ var import_arktype2 = require("arktype");
30
30
  var import_promises = require("fs/promises");
31
31
 
32
32
  // package.json
33
- var name = "typedapi";
34
- var version = "0.1.0";
33
+ var name = "typed-openapi";
34
+ var version = "0.1.1";
35
35
 
36
36
  // src/generator.ts
37
37
  var import_server2 = require("pastable/server");
@@ -288,14 +288,22 @@ var Box = class _Box {
288
288
  };
289
289
 
290
290
  // src/format.ts
291
- var import_formatter = require("@dprint/formatter");
292
- var fs = __toESM(require("fs"), 1);
293
- var import_path = require("path");
294
- var tsPluginPath = (0, import_path.join)(__dirname, "..", "node_modules", "@dprint", "typescript", "plugin.wasm");
295
- var buffer = fs.readFileSync(tsPluginPath);
296
- var formatter = (0, import_formatter.createFromBuffer)(buffer);
297
- formatter.setConfig({ lineWidth: 120, indentWidth: 2 }, {});
298
- var prettify = (code, options) => formatter.formatText("code.ts", code, options);
291
+ var import_prettier = __toESM(require("prettier"), 1);
292
+ var import_parser_typescript = __toESM(require("prettier/parser-typescript"), 1);
293
+ function maybePretty(input, options) {
294
+ try {
295
+ return import_prettier.default.format(input, {
296
+ parser: "typescript",
297
+ plugins: [import_parser_typescript.default],
298
+ ...options
299
+ });
300
+ } catch (err) {
301
+ console.warn("Failed to format code");
302
+ console.warn(err);
303
+ return input;
304
+ }
305
+ }
306
+ var prettify = (str, options) => maybePretty(str, { printWidth: 120, trailingComma: "all", ...options });
299
307
 
300
308
  // src/generator.ts
301
309
  var Codegen = __toESM(require("@sinclair/typebox-codegen"), 1);
@@ -354,9 +362,9 @@ var generateFile = (options) => {
354
362
  `;
355
363
  return prettify(file);
356
364
  };
357
- var generateSchemaList = ({ refs }) => {
365
+ var generateSchemaList = ({ refs, runtime }) => {
358
366
  let file = `
359
- export namespace Schemas {
367
+ ${runtime === "none" ? "export namespace Schemas {" : ""}
360
368
  // <Schemas>
361
369
  `;
362
370
  refs.getOrderedSchemas().forEach(([schema, infos]) => {
@@ -369,7 +377,7 @@ var generateSchemaList = ({ refs }) => {
369
377
  });
370
378
  return file + `
371
379
  // </Schemas>
372
- }
380
+ ${runtime === "none" ? "}" : ""}
373
381
  `;
374
382
  };
375
383
  var parameterObjectToString = (parameters) => {
@@ -384,7 +392,7 @@ var parameterObjectToString = (parameters) => {
384
392
  };
385
393
  var generateEndpointSchemaList = (ctx) => {
386
394
  let file = `
387
- export namespace Endpoints {
395
+ ${ctx.runtime === "none" ? "export namespace Endpoints {" : ""}
388
396
  // <Endpoints>
389
397
  ${ctx.runtime === "none" ? "" : "type __ENDPOINTS_START__ = {}"}
390
398
  `;
@@ -398,18 +406,18 @@ var generateEndpointSchemaList = (ctx) => {
398
406
  ${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
399
407
  ${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
400
408
  }` : "parameters: never,"}
401
- response: ${endpoint.response.recompute((box) => {
409
+ response: ${ctx.runtime === "none" ? endpoint.response.recompute((box) => {
402
410
  if (Box.isReference(box) && !box.params.generics) {
403
411
  box.value = `Schemas.${box.value}`;
404
412
  }
405
413
  return box;
406
- }).value},
414
+ }).value : endpoint.response.value},
407
415
  }
408
416
  `;
409
417
  });
410
418
  return file + `
411
419
  // </Endpoints>
412
- }
420
+ ${ctx.runtime === "none" ? "}" : ""}
413
421
  ${ctx.runtime === "none" ? "" : "type __ENDPOINTS_END__ = {}"}
414
422
  `;
415
423
  };
@@ -421,7 +429,9 @@ var generateEndpointByMethod = (ctx) => {
421
429
  export ${ctx.runtime === "none" ? "type" : "const"} EndpointByMethod = {
422
430
  ${Object.entries(byMethods).map(([method, list]) => {
423
431
  return `${method}: {
424
- ${list.map((endpoint) => `"${endpoint.path}": Endpoints.${endpoint.meta.alias}`)}
432
+ ${list.map(
433
+ (endpoint) => `"${endpoint.path}": ${ctx.runtime === "none" ? "Endpoints." : ""}${endpoint.meta.alias}`
434
+ )}
425
435
  }`;
426
436
  }).join(",\n")}
427
437
  }
package/dist/cli.js CHANGED
@@ -1,13 +1,13 @@
1
1
  // src/cli.ts
2
2
  import SwaggerParser from "@apidevtools/swagger-parser";
3
3
  import { cac } from "cac";
4
- import { join as join2 } from "pathe";
4
+ import { join } from "pathe";
5
5
  import { type as type2 } from "arktype";
6
6
  import { writeFile } from "fs/promises";
7
7
 
8
8
  // package.json
9
- var name = "typedapi";
10
- var version = "0.1.0";
9
+ var name = "typed-openapi";
10
+ var version = "0.1.1";
11
11
 
12
12
  // src/generator.ts
13
13
  import { capitalize as capitalize2, groupBy } from "pastable/server";
@@ -264,14 +264,22 @@ var Box = class _Box {
264
264
  };
265
265
 
266
266
  // src/format.ts
267
- import { createFromBuffer } from "@dprint/formatter";
268
- import * as fs from "fs";
269
- import { join } from "path";
270
- var tsPluginPath = join(__dirname, "..", "node_modules", "@dprint", "typescript", "plugin.wasm");
271
- var buffer = fs.readFileSync(tsPluginPath);
272
- var formatter = createFromBuffer(buffer);
273
- formatter.setConfig({ lineWidth: 120, indentWidth: 2 }, {});
274
- var prettify = (code, options) => formatter.formatText("code.ts", code, options);
267
+ import prettier from "prettier";
268
+ import parserTypescript from "prettier/parser-typescript";
269
+ function maybePretty(input, options) {
270
+ try {
271
+ return prettier.format(input, {
272
+ parser: "typescript",
273
+ plugins: [parserTypescript],
274
+ ...options
275
+ });
276
+ } catch (err) {
277
+ console.warn("Failed to format code");
278
+ console.warn(err);
279
+ return input;
280
+ }
281
+ }
282
+ var prettify = (str, options) => maybePretty(str, { printWidth: 120, trailingComma: "all", ...options });
275
283
 
276
284
  // src/generator.ts
277
285
  import * as Codegen from "@sinclair/typebox-codegen";
@@ -330,9 +338,9 @@ var generateFile = (options) => {
330
338
  `;
331
339
  return prettify(file);
332
340
  };
333
- var generateSchemaList = ({ refs }) => {
341
+ var generateSchemaList = ({ refs, runtime }) => {
334
342
  let file = `
335
- export namespace Schemas {
343
+ ${runtime === "none" ? "export namespace Schemas {" : ""}
336
344
  // <Schemas>
337
345
  `;
338
346
  refs.getOrderedSchemas().forEach(([schema, infos]) => {
@@ -345,7 +353,7 @@ var generateSchemaList = ({ refs }) => {
345
353
  });
346
354
  return file + `
347
355
  // </Schemas>
348
- }
356
+ ${runtime === "none" ? "}" : ""}
349
357
  `;
350
358
  };
351
359
  var parameterObjectToString = (parameters) => {
@@ -360,7 +368,7 @@ var parameterObjectToString = (parameters) => {
360
368
  };
361
369
  var generateEndpointSchemaList = (ctx) => {
362
370
  let file = `
363
- export namespace Endpoints {
371
+ ${ctx.runtime === "none" ? "export namespace Endpoints {" : ""}
364
372
  // <Endpoints>
365
373
  ${ctx.runtime === "none" ? "" : "type __ENDPOINTS_START__ = {}"}
366
374
  `;
@@ -374,18 +382,18 @@ var generateEndpointSchemaList = (ctx) => {
374
382
  ${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
375
383
  ${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
376
384
  }` : "parameters: never,"}
377
- response: ${endpoint.response.recompute((box) => {
385
+ response: ${ctx.runtime === "none" ? endpoint.response.recompute((box) => {
378
386
  if (Box.isReference(box) && !box.params.generics) {
379
387
  box.value = `Schemas.${box.value}`;
380
388
  }
381
389
  return box;
382
- }).value},
390
+ }).value : endpoint.response.value},
383
391
  }
384
392
  `;
385
393
  });
386
394
  return file + `
387
395
  // </Endpoints>
388
- }
396
+ ${ctx.runtime === "none" ? "}" : ""}
389
397
  ${ctx.runtime === "none" ? "" : "type __ENDPOINTS_END__ = {}"}
390
398
  `;
391
399
  };
@@ -397,7 +405,9 @@ var generateEndpointByMethod = (ctx) => {
397
405
  export ${ctx.runtime === "none" ? "type" : "const"} EndpointByMethod = {
398
406
  ${Object.entries(byMethods).map(([method, list]) => {
399
407
  return `${method}: {
400
- ${list.map((endpoint) => `"${endpoint.path}": Endpoints.${endpoint.meta.alias}`)}
408
+ ${list.map(
409
+ (endpoint) => `"${endpoint.path}": ${ctx.runtime === "none" ? "Endpoints." : ""}${endpoint.meta.alias}`
410
+ )}
401
411
  }`;
402
412
  }).join(",\n")}
403
413
  }
@@ -820,7 +830,7 @@ cli.command("<input>", "Generate").option("-o, --output <path>", "Output path fo
820
830
  const ctx = mapOpenApiEndpoints(openApiDoc);
821
831
  console.log(`Found ${ctx.endpointList.length} endpoints`);
822
832
  const content = generateFile({ ...ctx, runtime: options.runtime });
823
- const output = join2(
833
+ const output = join(
824
834
  cwd,
825
835
  options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`
826
836
  );
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typed-openapi",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
7
  "bin": {
@@ -14,19 +14,18 @@
14
14
  "dependencies": {
15
15
  "@apidevtools/swagger-parser": "^10.1.0",
16
16
  "@changesets/cli": "^2.26.2",
17
- "@dprint/formatter": "^0.2.0",
18
- "@dprint/typescript": "^0.85.1",
19
17
  "@sinclair/typebox-codegen": "^0.8.5",
20
18
  "arktype": "1.0.18-alpha",
21
19
  "cac": "^6.7.14",
22
- "dprint": "^0.40.2",
23
20
  "openapi3-ts": "^4.1.2",
24
21
  "pastable": "^2.2.1",
25
22
  "pathe": "^1.1.1",
23
+ "prettier": "2.8.4",
26
24
  "ts-pattern": "^5.0.4"
27
25
  },
28
26
  "devDependencies": {
29
27
  "@types/node": "^20.4.5",
28
+ "@types/prettier": "2.7.3",
30
29
  "tsup": "^7.1.0",
31
30
  "typescript": "^5.1.6",
32
31
  "vite-node": "^0.33.0",
@@ -47,6 +46,8 @@
47
46
  "dev": "tsup --watch",
48
47
  "build": "tsup",
49
48
  "test": "vitest",
50
- "typecheck": "tsc --noEmit"
49
+ "typecheck": "tsc --noEmit",
50
+ "release": "changeset publish",
51
+ "release-dev": "changeset version --snapshot dev && changeset publish --tag dev"
51
52
  }
52
53
  }
package/src/format.ts CHANGED
@@ -1,12 +1,20 @@
1
- import { createFromBuffer, GlobalConfiguration } from "@dprint/formatter";
2
- import * as fs from "fs";
3
- import { join } from "path";
1
+ import prettier, { type Options } from "prettier";
2
+ import parserTypescript from "prettier/parser-typescript";
4
3
 
5
- const tsPluginPath = join(__dirname, "..", "node_modules", "@dprint", "typescript", "plugin.wasm");
6
- const buffer = fs.readFileSync(tsPluginPath);
7
- const formatter = createFromBuffer(buffer);
4
+ /** @see https://github.dev/stephenh/ts-poet/blob/5ea0dbb3c9f1f4b0ee51a54abb2d758102eda4a2/src/Code.ts#L231 */
5
+ function maybePretty(input: string, options?: Options | null): string {
6
+ try {
7
+ return prettier.format(input, {
8
+ parser: "typescript",
9
+ plugins: [parserTypescript],
10
+ ...options,
11
+ });
12
+ } catch (err) {
13
+ console.warn("Failed to format code");
14
+ console.warn(err);
15
+ return input; // assume it's invalid syntax and ignore
16
+ }
17
+ }
8
18
 
9
- formatter.setConfig({ lineWidth: 120, indentWidth: 2 }, {});
10
-
11
- export const prettify = (code: string, options?: GlobalConfiguration) =>
12
- formatter.formatText("code.ts", code, options as any);
19
+ export const prettify = (str: string, options?: Options | null) =>
20
+ maybePretty(str, { printWidth: 120, trailingComma: "all", ...options });
package/src/generator.ts CHANGED
@@ -95,9 +95,9 @@ export const generateFile = (options: GeneratorOptions) => {
95
95
  return prettify(file);
96
96
  };
97
97
 
98
- const generateSchemaList = ({ refs }: GeneratorContext) => {
98
+ const generateSchemaList = ({ refs, runtime }: GeneratorContext) => {
99
99
  let file = `
100
- export namespace Schemas {
100
+ ${runtime === "none" ? "export namespace Schemas {" : ""}
101
101
  // <Schemas>
102
102
  `;
103
103
  refs.getOrderedSchemas().forEach(([schema, infos]) => {
@@ -111,7 +111,7 @@ const generateSchemaList = ({ refs }: GeneratorContext) => {
111
111
  file +
112
112
  `
113
113
  // </Schemas>
114
- }
114
+ ${runtime === "none" ? "}" : ""}
115
115
  `
116
116
  );
117
117
  };
@@ -127,7 +127,7 @@ const parameterObjectToString = (parameters: Box<BoxRef> | Record<string, AnyBox
127
127
  };
128
128
  const generateEndpointSchemaList = (ctx: GeneratorContext) => {
129
129
  let file = `
130
- export namespace Endpoints {
130
+ ${ctx.runtime === "none" ? "export namespace Endpoints {" : ""}
131
131
  // <Endpoints>
132
132
  ${ctx.runtime === "none" ? "" : "type __ENDPOINTS_START__ = {}"}
133
133
  `;
@@ -146,13 +146,15 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
146
146
  : "parameters: never,"
147
147
  }
148
148
  response: ${
149
- endpoint.response.recompute((box) => {
150
- if (Box.isReference(box) && !box.params.generics) {
151
- box.value = `Schemas.${box.value}`;
152
- }
153
-
154
- return box;
155
- }).value
149
+ ctx.runtime === "none"
150
+ ? endpoint.response.recompute((box) => {
151
+ if (Box.isReference(box) && !box.params.generics) {
152
+ box.value = `Schemas.${box.value}`;
153
+ }
154
+
155
+ return box;
156
+ }).value
157
+ : endpoint.response.value
156
158
  },
157
159
  }\n`;
158
160
  });
@@ -161,7 +163,7 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
161
163
  file +
162
164
  `
163
165
  // </Endpoints>
164
- }
166
+ ${ctx.runtime === "none" ? "}" : ""}
165
167
  ${ctx.runtime === "none" ? "" : "type __ENDPOINTS_END__ = {}"}
166
168
  `
167
169
  );
@@ -177,7 +179,9 @@ const generateEndpointByMethod = (ctx: GeneratorContext) => {
177
179
  ${Object.entries(byMethods)
178
180
  .map(([method, list]) => {
179
181
  return `${method}: {
180
- ${list.map((endpoint) => `"${endpoint.path}": Endpoints.${endpoint.meta.alias}`)}
182
+ ${list.map(
183
+ (endpoint) => `"${endpoint.path}": ${ctx.runtime === "none" ? "Endpoints." : ""}${endpoint.meta.alias}`,
184
+ )}
181
185
  }`;
182
186
  })
183
187
  .join(",\n")}