Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add API types configuration to base config #169

Merged
merged 1 commit into from
Sep 29, 2023
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
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ DEV_MODE=1
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6"
},
Expand Down Expand Up @@ -513,7 +513,7 @@ DEV_MODE=1
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6"
},
Expand Down Expand Up @@ -885,7 +885,7 @@ DEV_MODE=1
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6"
},
Expand Down Expand Up @@ -1240,7 +1240,7 @@ DEV_MODE=1
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/whook-create/src/services/createWhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('initCreateWhook', () => {
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6",
},
Expand Down Expand Up @@ -419,7 +419,7 @@ describe('initCreateWhook', () => {
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6",
},
Expand Down Expand Up @@ -626,7 +626,7 @@ describe('initCreateWhook', () => {
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"parse-gitignore": "^1.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"ts-node": "^10.8.1",
"typescript": "^5.1.6"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/whook-example/src/config/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const CONFIG: Omit<AppConfig, 'HOST'> = {
description: 'The provided token ("$0") do not match',
},
},
OPEN_API_TYPES_CONFIG: {
baseName: 'API',
generateUnusedSchemas: false,
generateRealEnums: false,
exportNamespaces: false,
brandedTypes: [],
},
JWT: {
duration: '2d',
tolerance: '2h',
Expand Down
4 changes: 4 additions & 0 deletions packages/whook-example/src/config/local/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const CONFIG: AppConfig = {
HOST: 'localhost',
DEV_ACCESS_TOKEN: 'admin|1|1',
DEFAULT_MECHANISM: 'Fake',
OPEN_API_TYPES_CONFIG: {
...COMMON_CONFIG.OPEN_API_TYPES_CONFIG,
generateUnusedSchemas: true,
},
// This allows you to map service names depending on
// the targetted environment. Here, for dev, we don't send SMS
// but instead log them in the console.
Expand Down
2 changes: 1 addition & 1 deletion packages/whook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"openapi-types": "^12.1.3",
"pkg-dir": "^7.0.0",
"portfinder": "^1.0.32",
"schema2dts": "^5.0.1",
"schema2dts": "^5.1.0",
"type-fest": "^4.2.0",
"yargs-parser": "^21.1.1",
"yerror": "^8.0.0"
Expand Down
11 changes: 9 additions & 2 deletions packages/whook/src/commands/generateOpenAPITypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { describe, it, beforeEach, jest, expect } from '@jest/globals';
import initGenerateOpenAPITypes from './generateOpenAPITypes.js';
import { PassThrough } from 'stream';
import { definition as initGetPingDefinition } from '../handlers/getPing.js';
import { NodeEnv } from 'application-services';
import type { OpenAPITypesGenerationOptions } from 'schema2dts';
import type { OpenAPIV3 } from 'openapi-types';
import type { LogService } from 'common-services';

describe('generateOpenAPITypes', () => {
const getOpenAPI = jest.fn();
const log = jest.fn<LogService>();
const OPEN_API_TYPES_CONFIG: OpenAPITypesGenerationOptions = {
baseName: 'API',
generateUnusedSchemas: true,
generateRealEnums: false,
exportNamespaces: false,
brandedTypes: [],
};
const API: OpenAPIV3.Document = {
openapi: '3.0.2',
info: {
Expand Down Expand Up @@ -40,7 +47,7 @@ describe('generateOpenAPITypes', () => {
outstream.once('end', () => resolve(buffer.toString()));
});
const generateOpenAPITypes = await initGenerateOpenAPITypes({
ENV: { NODE_ENV: NodeEnv.Development },
OPEN_API_TYPES_CONFIG,
instream,
outstream,
log,
Expand Down
19 changes: 8 additions & 11 deletions packages/whook/src/commands/generateOpenAPITypes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { autoService, extra } from 'knifecycle';
import { generateOpenAPITypes as generateTypes, toSource } from 'schema2dts';
import { NodeEnv } from 'application-services';
import type { AppEnvVars } from 'application-services';
import type { OpenAPITypesGenerationOptions } from 'schema2dts';
import type { LogService } from 'common-services';
import type { WhookCommandDefinition } from '../services/promptArgs.js';

export type OpenAPITypesConfig = {
OPEN_API_TYPES_CONFIG: OpenAPITypesGenerationOptions;
};

/* Architecture Note #5.3: Examples

Whook's default project comes with a few sample commands.
Expand All @@ -30,12 +33,11 @@ export const definition: WhookCommandDefinition = {
export default extra(definition, autoService(initGenerateOpenAPITypes));

async function initGenerateOpenAPITypes({
ENV,
OPEN_API_TYPES_CONFIG,
instream = process.stdin,
outstream = process.stdout,
log,
}: {
ENV: AppEnvVars;
}: OpenAPITypesConfig & {
instream: NodeJS.ReadableStream;
outstream: NodeJS.WritableStream;
log: LogService;
Expand All @@ -53,12 +55,7 @@ async function initGenerateOpenAPITypes({
});

const typesDefs = toSource(
await generateTypes(JSON.parse(openAPI), {
baseName: 'API',
generateUnusedSchemas: ENV.NODE_ENV === NodeEnv.Development,
generateRealEnums: false,
exportNamespaces: false,
}),
await generateTypes(JSON.parse(openAPI), OPEN_API_TYPES_CONFIG),
);

log('warning', '📇 - Writing types...');
Expand Down
4 changes: 3 additions & 1 deletion packages/whook/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
ProcessEnvConfig,
ProcessServiceConfig,
} from 'application-services';
import type { OpenAPITypesConfig } from './commands/generateOpenAPITypes.js';

export type WhookBaseEnv = WhookHTTPServerEnv &
WhookBaseURLEnv &
Expand All @@ -44,4 +45,5 @@ export type WhookBaseConfigs = ProcessServiceConfig &
WhookObfuscatorConfig &
WhookAPIDefinitionsConfig &
WhookCompilerConfig &
WhookWrappersConfig;
WhookWrappersConfig &
OpenAPITypesConfig;
4 changes: 3 additions & 1 deletion packages/whook/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export async function restartDevServer<T extends Dependencies>({

const {
ENV,
OPEN_API_TYPES_CONFIG,
PROJECT_SRC,
$instance: _instance,
delay: _delay,
Expand All @@ -125,6 +126,7 @@ export async function restartDevServer<T extends Dependencies>({
...injectedNames,

'ENV',
'OPEN_API_TYPES_CONFIG',
'PROJECT_SRC',
'$instance',
'delay',
Expand Down Expand Up @@ -157,7 +159,7 @@ export async function restartDevServer<T extends Dependencies>({
const bridge = new PassThrough();
const openAPITypesGenerationPromise = (
await initGenerateOpenAPITypes({
ENV,
OPEN_API_TYPES_CONFIG,
instream,
outstream: bridge,
log,
Expand Down