From cd3fb47e90e459bbd8b09fca4ee3a1301ee87ddc Mon Sep 17 00:00:00 2001 From: Noe TATOUD Date: Mon, 21 Oct 2024 14:05:10 +0200 Subject: [PATCH] feat(config): create defineConfig and add it to configure hook --- package.json | 2 ++ src/configure.ts | 6 ++++++ src/define_config.ts | 5 +++++ src/index.ts | 1 + src/stubs/config/openapi.stub | 13 +++++++++++++ src/stubs/index.ts | 3 +++ src/types/openapi.ts | 5 +++++ tests/configure.test.ts | 3 +++ tests/openapi_provider.test.ts | 10 +++++++++- tests/test-utils/app.ts | 8 +++++++- 10 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/define_config.ts create mode 100644 src/stubs/config/openapi.stub create mode 100644 src/stubs/index.ts create mode 100644 src/types/openapi.ts diff --git a/package.json b/package.json index df51dce..3d7d4f5 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,8 @@ "scripts": { "prepare": "husky", "build": "tsc", + "copy:templates": "cp -r src/stubs/ dist/src", + "postbuild": "pnpm run copy:templates", "build:clean": "rm -rf ./dist && pnpm run build", "format": "biome format --write ./", "check-format": "biome check ./", diff --git a/src/configure.ts b/src/configure.ts index 16b4769..17effc3 100644 --- a/src/configure.ts +++ b/src/configure.ts @@ -1,8 +1,14 @@ import type Configure from '@adonisjs/core/commands/configure'; +import { STUBS_ROOT } from './stubs/index.js'; export async function configure(command: Configure) { const codemods = await command.createCodemods(); + /** + * Create openapi config file + */ + await codemods.makeUsingStub(STUBS_ROOT, 'config/openapi.stub', {}); + /** * Register OpenapiServiceProvider to the rcfile */ diff --git a/src/define_config.ts b/src/define_config.ts new file mode 100644 index 0000000..79442b8 --- /dev/null +++ b/src/define_config.ts @@ -0,0 +1,5 @@ +import type { OpenAPIConfig } from './types/openapi.js'; + +export function defineConfig(config: OpenAPIConfig): OpenAPIConfig { + return config; +} diff --git a/src/index.ts b/src/index.ts index 011fd11..2681d13 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export { configure } from './configure.js'; +export { defineConfig } from './define_config.js'; diff --git a/src/stubs/config/openapi.stub b/src/stubs/config/openapi.stub new file mode 100644 index 0000000..778269f --- /dev/null +++ b/src/stubs/config/openapi.stub @@ -0,0 +1,13 @@ +{{{ + exports({ to: app.configPath('openapi.ts') }) +}}} +import { defineConfig } from 'adonis-openapi' + +const openapiConfig = defineConfig({ + infos: { + title: 'My AdonisJs API', + version: '0.0.1', + }, +}); + +export default openapiConfig diff --git a/src/stubs/index.ts b/src/stubs/index.ts new file mode 100644 index 0000000..3397c14 --- /dev/null +++ b/src/stubs/index.ts @@ -0,0 +1,3 @@ +import { joinToURL } from '@poppinss/utils'; + +export const STUBS_ROOT = joinToURL(import.meta.url, './'); diff --git a/src/types/openapi.ts b/src/types/openapi.ts new file mode 100644 index 0000000..5c2f8db --- /dev/null +++ b/src/types/openapi.ts @@ -0,0 +1,5 @@ +import type { OpenAPIV3_1 } from 'openapi-types'; + +export type OpenAPIConfig = { + infos: OpenAPIV3_1.InfoObject; +}; diff --git a/tests/configure.test.ts b/tests/configure.test.ts index 7d134fc..377f5ca 100644 --- a/tests/configure.test.ts +++ b/tests/configure.test.ts @@ -24,7 +24,10 @@ test.group('Configure', (group) => { await command.exec(); + await assert.fileExists('config/openapi.ts'); await assert.fileExists('adonisrc.ts'); + + await assert.fileContains('config/openapi.ts', 'defineConfig({'); await assert.fileContains('adonisrc.ts', 'adonis-openapi/openapi_provider'); }).disableTimeout(); }); diff --git a/tests/openapi_provider.test.ts b/tests/openapi_provider.test.ts index e67bcbb..5147e44 100644 --- a/tests/openapi_provider.test.ts +++ b/tests/openapi_provider.test.ts @@ -1,10 +1,18 @@ import { test } from '@japa/runner'; +import { defineConfig } from '../src/define_config.js'; import OpenApiStore from '../src/features/openapi_store.js'; import { createApp } from './test-utils/app.js'; test.group('OpenAPI Provider', () => { test('register openapi provider', async ({ assert }) => { - const app = await createApp(); + const app = await createApp( + defineConfig({ + infos: { + title: 'Test', + version: '1.0.0', + }, + }), + ); assert.instanceOf(await app.container.make('openapi.store'), OpenApiStore); }); diff --git a/tests/test-utils/app.ts b/tests/test-utils/app.ts index 323f591..27c23bf 100644 --- a/tests/test-utils/app.ts +++ b/tests/test-utils/app.ts @@ -1,5 +1,6 @@ import { IgnitorFactory } from '@adonisjs/core/factories'; import { BASE_URL } from '../../bin/test.js'; +import type { OpenAPIConfig } from '../../src/types/openapi.js'; const IMPORTER = (filePath: string) => { if (filePath.startsWith('./') || filePath.startsWith('../')) { @@ -8,7 +9,7 @@ const IMPORTER = (filePath: string) => { return import(filePath); }; -export const createApp = async () => { +export const createApp = async (openapiConfig?: OpenAPIConfig) => { const ignitor = new IgnitorFactory() .merge({ rcFileContents: { @@ -17,6 +18,11 @@ export const createApp = async () => { }) .withCoreConfig() .withCoreProviders() + .merge({ + config: { + openapi: openapiConfig, + }, + }) .create(BASE_URL, { importer: IMPORTER }); const app = ignitor.createApp('web');