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(@whook/whook): allow to filter the API #172

Merged
merged 1 commit into from
Nov 7, 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
8 changes: 4 additions & 4 deletions packages/whook-example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('runServer', () => {
"💿 - Loading "API" initializer from "/home/whoiam/projects/whook/packages/whook-example/src/services/API.ts".",
],
[
"💿 - Loading "FILTER_API_TAGS" initializer from "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_TAGS.ts".",
"💿 - Loading "FILTER_API_DEFINITION" initializer from "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_DEFINITION.ts".",
],
[
"💿 - Loading "MECHANISMS" initializer from "/home/whoiam/projects/whook/packages/whook-example/src/services/MECHANISMS.ts".",
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('runServer', () => {
"💿 - Service "API" found in "/home/whoiam/projects/whook/packages/whook-example/src/services/API.ts".",
],
[
"💿 - Service "FILTER_API_TAGS" found in "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_TAGS.ts".",
"💿 - Service "FILTER_API_DEFINITION" found in "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_DEFINITION.ts".",
],
[
"💿 - Service "MECHANISMS" found in "/home/whoiam/projects/whook/packages/whook-example/src/services/MECHANISMS.ts".",
Expand Down Expand Up @@ -580,7 +580,7 @@ describe('runServer', () => {
"🛂 - Dynamic import of "/home/whoiam/projects/whook/packages/whook-example/src/services/API.ts".",
],
[
"🛂 - Dynamic import of "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_TAGS.ts".",
"🛂 - Dynamic import of "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_DEFINITION.ts".",
],
[
"🛂 - Dynamic import of "/home/whoiam/projects/whook/packages/whook-example/src/services/MECHANISMS.ts".",
Expand Down Expand Up @@ -640,7 +640,7 @@ describe('runServer', () => {
"🛂 - Resolving "/home/whoiam/projects/whook/packages/whook-example/src/services/API.ts".",
],
[
"🛂 - Resolving "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_TAGS.ts".",
"🛂 - Resolving "/home/whoiam/projects/whook/packages/whook-example/src/services/FILTER_API_DEFINITION.ts".",
],
[
"🛂 - Resolving "/home/whoiam/projects/whook/packages/whook-example/src/services/MECHANISMS.ts".",
Expand Down
86 changes: 86 additions & 0 deletions packages/whook-example/src/services/FILTER_API_DEFINITION.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
import initFilterAPIDefinition from './FILTER_API_DEFINITION.js';
import type { LogService } from 'common-services';

describe('initFilterAPIDefinition', () => {
describe('should work', () => {
const log = jest.fn<LogService>();

beforeEach(() => {
log.mockClear();
});

test('with empty ENV', async () => {
const FILTER_API_DEFINITION = await initFilterAPIDefinition({
ENV: {},
log,
});

expect(
FILTER_API_DEFINITION({
path: '/test',
method: 'get',
operation: {
operationId: 'test',
parameters: [],
tags: ['test'],
responses: {},
},
}),
).toBeFalsy();
expect({
logCalls: log.mock.calls,
}).toMatchInlineSnapshot(`
{
"logCalls": [],
}
`);
});

test('with some tags in ENV', async () => {
const FILTER_API_DEFINITION = await initFilterAPIDefinition({
ENV: {
FILTER_API_TAGS: 'test,test2',
},
log,
});

expect(
FILTER_API_DEFINITION({
path: '/test',
method: 'get',
operation: {
operationId: 'test',
parameters: [],
tags: ['test'],
responses: {},
},
}),
).toBeFalsy();
expect(
FILTER_API_DEFINITION({
path: '/test',
method: 'get',
operation: {
operationId: 'test3',
parameters: [],
tags: ['test3'],
responses: {},
},
}),
).toBeTruthy();
expect({
logCalls: log.mock.calls,
}).toMatchInlineSnapshot(`
{
"logCalls": [
[
"warning",
"⏳ - Filtering API with (test,test2) tags!",
],
],
}
`);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { name, autoService } from 'knifecycle';
import { noop, identity } from '@whook/whook';
import type { LogService } from 'common-services';
import type {
WhookAPIHandlerDefinition,
WhookAPIDefinitionFilter,
} from '@whook/whook';

export default name('FILTER_API_TAGS', autoService(initFilterAPITags));
export default name(
'FILTER_API_DEFINITION',
autoService(initFilterAPIDefinition),
);

export type FilterAPITagsEnvVars = {
export type FilterAPIDefinitionEnvVars = {
FILTER_API_TAGS?: string;
};

Expand All @@ -21,20 +28,25 @@ For example, to create a server with only `system` and
FILTER_API_TAGS=system,example npm start
```
*/
async function initFilterAPITags({
async function initFilterAPIDefinition({
ENV,
log = noop,
}: {
ENV: FilterAPITagsEnvVars;
ENV: FilterAPIDefinitionEnvVars;
log: LogService;
}): Promise<string[]> {
}): Promise<WhookAPIDefinitionFilter> {
const FILTER_API_TAGS = (ENV.FILTER_API_TAGS || '')
.split(',')
.filter(identity);

if (FILTER_API_TAGS.length > 0) {
log('warning', `⏳ - Filtering API with (${FILTER_API_TAGS}) tags!`);
return (definition: WhookAPIHandlerDefinition) => {
return !FILTER_API_TAGS.some((tag) =>
(definition.operation.tags || []).includes(tag),
);
};
}

return FILTER_API_TAGS;
return () => false;
}
48 changes: 0 additions & 48 deletions packages/whook-example/src/services/FILTER_API_TAGS.test.ts

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions packages/whook-example/src/whook.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import type { APIConfig } from './services/API.js';
import type { JWTServiceConfig } from 'jwt-service';
import type { BaseAppEnvVars } from 'application-services';
import type { JWTEnvVars } from 'jwt-service';
import type { FilterAPITagsEnvVars } from './services/FILTER_API_TAGS.ts';
import type { FilterAPIDefinitionEnvVars } from './services/FILTER_API_DEFINITION.ts';

declare module 'application-services' {
// Eventually override the process env type here
export interface AppEnvVars
extends BaseAppEnvVars,
WhookBaseEnv,
JWTEnvVars,
FilterAPITagsEnvVars,
FilterAPIDefinitionEnvVars,
WhookSwaggerUIEnv {
DRY_RUN?: string;
}
Expand Down
1 change: 1 addition & 0 deletions packages/whook/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export {
DEFAULT_IGNORED_FILES_SUFFIXES,
DEFAULT_REDUCED_FILES_SUFFIXES,
type WhookAPIDefinitionsConfig,
type WhookAPIDefinitionFilter,
} from './services/API_DEFINITIONS.js';
import initLoggerService from './services/logger.js';
import initExitService from './services/exit.js';
Expand Down
Loading