Skip to content

Feat: defineConfig and default config for configure hook #2

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./",
Expand Down
6 changes: 6 additions & 0 deletions src/configure.ts
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down
5 changes: 5 additions & 0 deletions src/define_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { OpenAPIConfig } from './types/openapi.js';

export function defineConfig(config: OpenAPIConfig): OpenAPIConfig {
return config;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { configure } from './configure.js';
export { defineConfig } from './define_config.js';
13 changes: 13 additions & 0 deletions src/stubs/config/openapi.stub
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/stubs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { joinToURL } from '@poppinss/utils';

export const STUBS_ROOT = joinToURL(import.meta.url, './');
5 changes: 5 additions & 0 deletions src/types/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { OpenAPIV3_1 } from 'openapi-types';

export type OpenAPIConfig = {
infos: OpenAPIV3_1.InfoObject;
};
3 changes: 3 additions & 0 deletions tests/configure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
10 changes: 9 additions & 1 deletion tests/openapi_provider.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});

Expand Down
8 changes: 7 additions & 1 deletion tests/test-utils/app.ts
Original file line number Diff line number Diff line change
@@ -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('../')) {
Expand All @@ -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: {
Expand All @@ -17,6 +18,11 @@ export const createApp = async () => {
})
.withCoreConfig()
.withCoreProviders()
.merge({
config: {
openapi: openapiConfig,
},
})
.create(BASE_URL, { importer: IMPORTER });

const app = ignitor.createApp('web');
Expand Down