This repository was archived by the owner on Jul 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add generateErrors script and update dependencies (MAPCO-8468) #47
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,129 @@ | ||||||
| import fs from 'node:fs/promises'; | ||||||
| import { parseArgs } from 'node:util'; | ||||||
| import path from 'node:path'; | ||||||
| import { dereference } from '@apidevtools/json-schema-ref-parser'; | ||||||
| import { format, resolveConfig } from 'prettier'; | ||||||
| import * as changeCase from 'change-case'; | ||||||
| import type { OpenAPI3, OperationObject, ResponseObject, SchemaObject } from 'openapi-typescript'; | ||||||
|
|
||||||
| const ARGS_SLICE = 2; | ||||||
|
|
||||||
| const { | ||||||
| values: { format: shouldFormat }, | ||||||
| positionals, | ||||||
| } = parseArgs({ | ||||||
| args: process.argv.slice(ARGS_SLICE), | ||||||
| options: { | ||||||
| format: { type: 'boolean', alias: 'f' }, | ||||||
| }, | ||||||
| allowPositionals: true, | ||||||
| }); | ||||||
|
|
||||||
| const [openapiPath, destinationPath] = positionals; | ||||||
|
|
||||||
| if (openapiPath === undefined || destinationPath === undefined) { | ||||||
| console.error('Usage: generateErrors <openapiPath> <destinationPath>'); | ||||||
| process.exit(1); | ||||||
| } | ||||||
|
|
||||||
| const openapi = await dereference<OpenAPI3>(openapiPath); | ||||||
|
|
||||||
| if (openapi.paths === undefined) { | ||||||
| console.error('No paths found in the OpenAPI document.'); | ||||||
| process.exit(1); | ||||||
| } | ||||||
|
|
||||||
| const errorCodes = new Set<string>(); | ||||||
|
|
||||||
| function extractCodeFromSchema(schema: SchemaObject): void { | ||||||
| // Handle direct code property | ||||||
| if (schema.type === 'object' && schema.properties?.code) { | ||||||
| const codeProperty = schema.properties.code as SchemaObject; | ||||||
|
|
||||||
| // Handle enum values | ||||||
| if (codeProperty.enum) { | ||||||
| codeProperty.enum.map(String).forEach((code) => { | ||||||
| errorCodes.add(code); | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Handle allOf combinations | ||||||
| if (schema.allOf) { | ||||||
| for (const subSchema of schema.allOf) { | ||||||
| extractCodeFromSchema(subSchema as SchemaObject); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Handle oneOf combinations | ||||||
| if (schema.oneOf) { | ||||||
| for (const subSchema of schema.oneOf) { | ||||||
| extractCodeFromSchema(subSchema as SchemaObject); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Handle anyOf combinations | ||||||
| if (schema.anyOf) { | ||||||
| for (const subSchema of schema.anyOf) { | ||||||
| extractCodeFromSchema(subSchema as SchemaObject); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function createError(code: string): string { | ||||||
| let className = changeCase.pascalCase(code); | ||||||
|
|
||||||
| if (!className.endsWith('Error')) { | ||||||
| className += 'Error'; | ||||||
| } | ||||||
|
|
||||||
| return `export class ${className} extends Error { | ||||||
| public readonly code = '${code}'; | ||||||
| /** | ||||||
| * Creates an instance of ${className}. | ||||||
| * @param message - The error message. | ||||||
| * @param cause - Optional original error or server response data. | ||||||
| */ | ||||||
| public constructor(message: string, cause?: unknown) { | ||||||
| super(message, { cause }); | ||||||
| Object.setPrototypeOf(this, new.target.prototype); | ||||||
| } | ||||||
| };\n`; | ||||||
| } | ||||||
|
|
||||||
| for (const [, methods] of Object.entries(openapi.paths)) { | ||||||
| for (const [key, operation] of Object.entries(methods) as [string, OperationObject][]) { | ||||||
| if (['servers', 'parameters'].includes(key)) { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| for (const [statusCode, response] of Object.entries(operation.responses ?? {}) as [string, ResponseObject][]) { | ||||||
| if (statusCode.startsWith('2') || statusCode.startsWith('3')) { | ||||||
| continue; // Skip successful and redirection responses | ||||||
| } | ||||||
|
|
||||||
| const schema = response.content?.['application/json']?.schema as SchemaObject | undefined; | ||||||
| if (schema) { | ||||||
| extractCodeFromSchema(schema); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (errorCodes.size === 0) { | ||||||
| console.warn('No error codes found in the OpenAPI document.'); | ||||||
| process.exit(0); | ||||||
| } | ||||||
|
|
||||||
| let errorFile = errorCodes.values().map(createError).toArray().join('\n'); | ||||||
|
||||||
| let errorFile = errorCodes.values().map(createError).toArray().join('\n'); | |
| let errorFile = Array.from(errorCodes).map(createError).join('\n'); |
Copilot
AI
Aug 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardcoded path './src/index.ts' for Prettier config resolution is inflexible. Consider using the current working directory or making this configurable.
Suggested change
| const prettierOptions = await resolveConfig('./src/index.ts'); | |
| const prettierOptions = await resolveConfig(destinationPath); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| { | ||
| "extends": "@map-colonies/tsconfig/tsconfig-library", | ||
| "compilerOptions": { | ||
| "lib": ["ESNext"] | ||
| }, | ||
| "include": ["src", "tests"], | ||
| "exclude": ["dist", "node_modules"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type assertion without validation could cause runtime errors. Consider checking if
schema.properties.codeis actually a SchemaObject before casting.