@map-colonies/openapi-helpers 2.1.0 → 3.1.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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,102 @@
1
+ import fs from 'node:fs/promises';
2
+ import { parseArgs } from 'node:util';
3
+ import path from 'node:path';
4
+ import { dereference } from '@apidevtools/json-schema-ref-parser';
5
+ import { format, resolveConfig } from 'prettier';
6
+ import * as changeCase from 'change-case';
7
+ const ARGS_SLICE = 2;
8
+ const { values: { format: shouldFormat }, positionals, } = parseArgs({
9
+ args: process.argv.slice(ARGS_SLICE),
10
+ options: {
11
+ format: { type: 'boolean', alias: 'f' },
12
+ },
13
+ allowPositionals: true,
14
+ });
15
+ const [openapiPath, destinationPath] = positionals;
16
+ if (openapiPath === undefined || destinationPath === undefined) {
17
+ console.error('Usage: generateErrors <openapiPath> <destinationPath>');
18
+ process.exit(1);
19
+ }
20
+ const openapi = await dereference(openapiPath);
21
+ if (openapi.paths === undefined) {
22
+ console.error('No paths found in the OpenAPI document.');
23
+ process.exit(1);
24
+ }
25
+ const errorCodes = new Set();
26
+ function extractCodeFromSchema(schema) {
27
+ // Handle direct code property
28
+ if (schema.type === 'object' && schema.properties?.code) {
29
+ const codeProperty = schema.properties.code;
30
+ // Handle enum values
31
+ if (codeProperty.enum) {
32
+ codeProperty.enum.map(String).forEach((code) => {
33
+ errorCodes.add(code);
34
+ });
35
+ }
36
+ }
37
+ // Handle allOf combinations
38
+ if (schema.allOf) {
39
+ for (const subSchema of schema.allOf) {
40
+ extractCodeFromSchema(subSchema);
41
+ }
42
+ }
43
+ // Handle oneOf combinations
44
+ if (schema.oneOf) {
45
+ for (const subSchema of schema.oneOf) {
46
+ extractCodeFromSchema(subSchema);
47
+ }
48
+ }
49
+ // Handle anyOf combinations
50
+ if (schema.anyOf) {
51
+ for (const subSchema of schema.anyOf) {
52
+ extractCodeFromSchema(subSchema);
53
+ }
54
+ }
55
+ }
56
+ function createError(code) {
57
+ let className = changeCase.pascalCase(code);
58
+ if (!className.endsWith('Error')) {
59
+ className += 'Error';
60
+ }
61
+ return `export class ${className} extends Error {
62
+ public readonly code = '${code}';
63
+ /**
64
+ * Creates an instance of ${className}.
65
+ * @param message - The error message.
66
+ * @param cause - Optional original error or server response data.
67
+ */
68
+ public constructor(message: string, cause?: unknown) {
69
+ super(message, { cause });
70
+ Object.setPrototypeOf(this, new.target.prototype);
71
+ }
72
+ };\n`;
73
+ }
74
+ for (const [, methods] of Object.entries(openapi.paths)) {
75
+ for (const [key, operation] of Object.entries(methods)) {
76
+ if (['servers', 'parameters'].includes(key)) {
77
+ continue;
78
+ }
79
+ for (const [statusCode, response] of Object.entries(operation.responses ?? {})) {
80
+ if (statusCode.startsWith('2') || statusCode.startsWith('3')) {
81
+ continue; // Skip successful and redirection responses
82
+ }
83
+ const schema = response.content?.['application/json']?.schema;
84
+ if (schema) {
85
+ extractCodeFromSchema(schema);
86
+ }
87
+ }
88
+ }
89
+ }
90
+ if (errorCodes.size === 0) {
91
+ console.warn('No error codes found in the OpenAPI document.');
92
+ process.exit(0);
93
+ }
94
+ let errorFile = errorCodes.values().map(createError).toArray().join('\n');
95
+ if (shouldFormat === true) {
96
+ const prettierOptions = await resolveConfig('./src/index.ts');
97
+ errorFile = await format(errorFile, { ...prettierOptions, parser: 'typescript' });
98
+ }
99
+ const directory = path.dirname(destinationPath);
100
+ await fs.mkdir(directory, { recursive: true });
101
+ await fs.writeFile(destinationPath, errorFile);
102
+ //# sourceMappingURL=generateErrors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateErrors.mjs","sourceRoot":"","sources":["../../src/generator/generateErrors.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,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,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;AAE/C,MAAM,EAAE,CAAC,SAAS,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@map-colonies/openapi-helpers",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "description": "A package that provides utilities for working with openapi files",
5
5
  "exports": {
6
6
  "./requestSender": {
@@ -45,13 +45,15 @@
45
45
  "access": "public"
46
46
  },
47
47
  "engines": {
48
- "node": ">=20"
48
+ "node": ">=22"
49
49
  },
50
50
  "homepage": "https://github.com/MapColonies/openapi-helpers#readme",
51
51
  "dependencies": {
52
- "oas-normalize": "^11.1.1",
53
- "ts-essentials": "^10.0.1",
54
- "yaml": "^2.5.0"
52
+ "@apidevtools/json-schema-ref-parser": "^14.1.1",
53
+ "change-case": "^5.4.4",
54
+ "oas-normalize": "^14.1.2",
55
+ "ts-essentials": "^10.1.1",
56
+ "yaml": "^2.8.0"
55
57
  },
56
58
  "peerDependencies": {
57
59
  "@types/express": "^4.17.21",
@@ -60,28 +62,27 @@
60
62
  "supertest": "^7.0.0"
61
63
  },
62
64
  "devDependencies": {
63
- "@commitlint/cli": "^19.6.1",
64
- "@map-colonies/commitlint-config": "^1.1.0",
65
+ "@commitlint/cli": "^19.8.1",
66
+ "@map-colonies/commitlint-config": "^1.1.1",
65
67
  "@map-colonies/eslint-config": "^6.0.0",
66
- "@map-colonies/infra-copilot-instructions": "^1.0.0",
67
- "@map-colonies/tsconfig": "^1.0.0",
68
- "@swc/core": "^1.7.26",
69
- "@swc/jest": "^0.2.36",
70
- "@types/jest": "^29.5.12",
71
- "@types/node": "^22.2.0",
72
- "@types/supertest": "^6.0.2",
73
- "body-parser": "^1.20.2",
74
- "commitlint": "^19.6.1",
75
- "cz-conventional-changelog": "^3.3.0",
68
+ "@map-colonies/infra-copilot-instructions": "^1.2.0",
69
+ "@map-colonies/tsconfig": "^1.0.1",
70
+ "@swc/core": "^1.13.2",
71
+ "@swc/jest": "^0.2.39",
72
+ "@types/jest": "^30.0.0",
73
+ "@types/node": "^24.1.0",
74
+ "@types/supertest": "^6.0.3",
75
+ "body-parser": "^2.2.0",
76
+ "commitlint": "^19.8.1",
76
77
  "eslint": "^9.22.0",
77
- "eslint-plugin-jest": "^28.11.0",
78
- "expect-type": "^0.19.0",
79
- "express": "^4.17.1",
78
+ "eslint-plugin-jest": "^28.14.0",
79
+ "expect-type": "^1.2.2",
80
+ "express": "^5.1.0",
80
81
  "husky": "^9.1.7",
81
- "jest": "^29.7.0",
82
- "pretty-quick": "^4.0.0",
82
+ "jest": "^30.0.5",
83
+ "pretty-quick": "^4.2.2",
83
84
  "rimraf": "^6.0.1",
84
- "typedoc": "^0.27.6",
85
- "typescript": "^5.7.3"
85
+ "typedoc": "^0.28.7",
86
+ "typescript": "^5.8.3"
86
87
  }
87
88
  }