Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions packages/openapi-generator/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#!/usr/bin/env node

import { command, run, option, string, flag, boolean, positional } from 'cmd-ts';
import {
command,
run,
option,
string,
optional,
flag,
boolean,
positional,
} from 'cmd-ts';
import * as E from 'fp-ts/Either';
import * as fs from 'fs';
import * as p from 'path';
Expand All @@ -11,6 +20,7 @@ import { convertRoutesToOpenAPI } from './openapi';
import type { Route } from './route';
import type { Schema } from './ir';
import { Project } from './project';
import { KNOWN_IMPORTS } from './knownImports';

const app = command({
name: 'api-ts',
Expand Down Expand Up @@ -57,11 +67,30 @@ const app = command({
short: 'i',
defaultValue: () => false,
}),
codecFile: option({
type: optional(string),
description: 'Custom codec definition file',
long: 'codec-file',
short: 'c',
defaultValue: () => undefined,
}),
},
handler: async ({ input, name, version }) => {
handler: async ({ input, name, version, codecFile }) => {
const filePath = p.resolve(input);

const project = await new Project().parseEntryPoint(filePath);
let knownImports = KNOWN_IMPORTS;
if (codecFile !== undefined) {
const codecFilePath = p.resolve(codecFile);
const codecModule = await import(codecFilePath);
if (codecModule.default === undefined) {
console.error(`Could not find default export in ${codecFilePath}`);
process.exit(1);
}
const customCodecs = codecModule.default(E);
knownImports = { ...knownImports, ...customCodecs };
}

const project = await new Project({}, knownImports).parseEntryPoint(filePath);
if (E.isLeft(project)) {
console.error(project.left);
process.exit(1);
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-generator/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Project } from './project';
import { findSymbolInitializer, resolveLiteralOrIdentifier } from './resolveInit';
import type { SourceFile } from './sourceFile';

import { KNOWN_IMPORTS, type KnownCodec } from './knownImports';
import type { KnownCodec } from './knownImports';

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

Expand All @@ -32,7 +32,7 @@ function codecIdentifier(
} else if (imp.type === 'star') {
return E.left(`Tried to use star import as codec ${id.value}`);
}
const knownImport = KNOWN_IMPORTS[imp.from]?.[imp.importedName];
const knownImport = project.knownImports[imp.from]?.[imp.importedName];
if (knownImport !== undefined) {
return E.right({ type: 'codec', schema: knownImport });
}
Expand Down Expand Up @@ -67,7 +67,7 @@ function codecIdentifier(
}

const name = id.property.value;
const knownImport = KNOWN_IMPORTS[objectSym.from]?.[name];
const knownImport = project.knownImports[objectSym.from]?.[name];
if (knownImport !== undefined) {
return E.right({ type: 'codec', schema: knownImport });
}
Expand Down
6 changes: 5 additions & 1 deletion packages/openapi-generator/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { promisify } from 'util';
import * as E from 'fp-ts/Either';
import resolve from 'resolve';

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

const readFile = promisify(fs.readFile);

export class Project {
readonly knownImports: Record<string, Record<string, KnownCodec>>;

private files: Record<string, SourceFile>;

constructor(files: Record<string, SourceFile> = {}) {
constructor(files: Record<string, SourceFile> = {}, knownImports = KNOWN_IMPORTS) {
this.files = files;
this.knownImports = knownImports;
}

add(path: string, sourceFile: SourceFile): void {
Expand Down