Skip to content

Commit f644b1c

Browse files
Merge pull request #532 from bitgopatmcl/custom-known-imports
Allow custom known codecs to be defined in a config file
2 parents 6bd251d + 8c5662f commit f644b1c

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

packages/openapi-generator/src/cli.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#!/usr/bin/env node
22

3-
import { command, run, option, string, flag, boolean, positional } from 'cmd-ts';
3+
import {
4+
command,
5+
run,
6+
option,
7+
string,
8+
optional,
9+
flag,
10+
boolean,
11+
positional,
12+
} from 'cmd-ts';
413
import * as E from 'fp-ts/Either';
514
import * as fs from 'fs';
615
import * as p from 'path';
@@ -11,6 +20,7 @@ import { convertRoutesToOpenAPI } from './openapi';
1120
import type { Route } from './route';
1221
import type { Schema } from './ir';
1322
import { Project } from './project';
23+
import { KNOWN_IMPORTS } from './knownImports';
1424

1525
const app = command({
1626
name: 'api-ts',
@@ -57,11 +67,30 @@ const app = command({
5767
short: 'i',
5868
defaultValue: () => false,
5969
}),
70+
codecFile: option({
71+
type: optional(string),
72+
description: 'Custom codec definition file',
73+
long: 'codec-file',
74+
short: 'c',
75+
defaultValue: () => undefined,
76+
}),
6077
},
61-
handler: async ({ input, name, version }) => {
78+
handler: async ({ input, name, version, codecFile }) => {
6279
const filePath = p.resolve(input);
6380

64-
const project = await new Project().parseEntryPoint(filePath);
81+
let knownImports = KNOWN_IMPORTS;
82+
if (codecFile !== undefined) {
83+
const codecFilePath = p.resolve(codecFile);
84+
const codecModule = await import(codecFilePath);
85+
if (codecModule.default === undefined) {
86+
console.error(`Could not find default export in ${codecFilePath}`);
87+
process.exit(1);
88+
}
89+
const customCodecs = codecModule.default(E);
90+
knownImports = { ...knownImports, ...customCodecs };
91+
}
92+
93+
const project = await new Project({}, knownImports).parseEntryPoint(filePath);
6594
if (E.isLeft(project)) {
6695
console.error(project.left);
6796
process.exit(1);

packages/openapi-generator/src/codec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { Project } from './project';
99
import { findSymbolInitializer, resolveLiteralOrIdentifier } from './resolveInit';
1010
import type { SourceFile } from './sourceFile';
1111

12-
import { KNOWN_IMPORTS, type KnownCodec } from './knownImports';
12+
import type { KnownCodec } from './knownImports';
1313

1414
type ResolvedRef = { type: 'ref'; name: string; location: string };
1515

@@ -32,7 +32,7 @@ function codecIdentifier(
3232
} else if (imp.type === 'star') {
3333
return E.left(`Tried to use star import as codec ${id.value}`);
3434
}
35-
const knownImport = KNOWN_IMPORTS[imp.from]?.[imp.importedName];
35+
const knownImport = project.knownImports[imp.from]?.[imp.importedName];
3636
if (knownImport !== undefined) {
3737
return E.right({ type: 'codec', schema: knownImport });
3838
}
@@ -67,7 +67,7 @@ function codecIdentifier(
6767
}
6868

6969
const name = id.property.value;
70-
const knownImport = KNOWN_IMPORTS[objectSym.from]?.[name];
70+
const knownImport = project.knownImports[objectSym.from]?.[name];
7171
if (knownImport !== undefined) {
7272
return E.right({ type: 'codec', schema: knownImport });
7373
}

packages/openapi-generator/src/project.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import { promisify } from 'util';
44
import * as E from 'fp-ts/Either';
55
import resolve from 'resolve';
66

7+
import { KNOWN_IMPORTS, type KnownCodec } from './knownImports';
78
import { parseSource, type SourceFile } from './sourceFile';
89

910
const readFile = promisify(fs.readFile);
1011

1112
export class Project {
13+
readonly knownImports: Record<string, Record<string, KnownCodec>>;
14+
1215
private files: Record<string, SourceFile>;
1316

14-
constructor(files: Record<string, SourceFile> = {}) {
17+
constructor(files: Record<string, SourceFile> = {}, knownImports = KNOWN_IMPORTS) {
1518
this.files = files;
19+
this.knownImports = knownImports;
1620
}
1721

1822
add(path: string, sourceFile: SourceFile): void {

0 commit comments

Comments
 (0)