-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(watch): factorize the watch code into some commands
Allows to generate types from commands, use `npm run watch` for the watch mode instead and adds some docs.
- Loading branch information
Showing
14 changed files
with
374 additions
and
25 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
packages/whook-create/src/services/__snapshots__/createWhook.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { watchDevServer } from '../src/dev'; | ||
const { runServer } = require('../src/index'); | ||
|
||
watchDevServer(); | ||
runServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { watchDevServer } from '../src/watch'; | ||
|
||
watchDevServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/whook-example/src/commands/generateOpenAPISchema.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import initGenerateOpenAPISchema from './generateOpenAPISchema'; | ||
import { PassThrough } from 'stream'; | ||
import type { WhookCommandArgs } from '@whook/cli'; | ||
|
||
describe('generateOpenAPISchema', () => { | ||
const getOpenAPI = jest.fn(); | ||
const log = jest.fn(); | ||
|
||
beforeEach(() => { | ||
getOpenAPI.mockReset(); | ||
log.mockReset(); | ||
}); | ||
|
||
it('should work', async () => { | ||
getOpenAPI.mockResolvedValueOnce({ | ||
status: 200, | ||
body: { | ||
openapi: '3.0.2', | ||
info: { | ||
version: '0.0.0', | ||
title: 'api', | ||
description: 'The API', | ||
}, | ||
}, | ||
}); | ||
|
||
const outstream = new PassThrough(); | ||
const outputPromise = new Promise((resolve, reject) => { | ||
let buffer = Buffer.from(''); | ||
outstream.on('data', (aBuffer) => { | ||
buffer = Buffer.concat([buffer, aBuffer]); | ||
}); | ||
outstream.once('error', () => reject); | ||
outstream.once('end', () => resolve(buffer.toString())); | ||
}); | ||
const generateOpenAPISchema = await initGenerateOpenAPISchema({ | ||
log, | ||
getOpenAPI, | ||
outstream, | ||
args: (Object.assign( | ||
{ | ||
pretty: true, | ||
}, | ||
{ | ||
_: ['generateOpenAPISchema'], | ||
}, | ||
) as unknown) as WhookCommandArgs, | ||
}); | ||
const result = await generateOpenAPISchema(); | ||
|
||
expect({ | ||
result, | ||
output: await outputPromise, | ||
getOpenAPICalls: getOpenAPI.mock.calls, | ||
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), | ||
}).toMatchInlineSnapshot( | ||
{}, | ||
` | ||
Object { | ||
"getOpenAPICalls": Array [ | ||
Array [ | ||
Object { | ||
"authenticated": true, | ||
"mutedMethods": Array [ | ||
"options", | ||
], | ||
"mutedParameters": Array [], | ||
}, | ||
], | ||
], | ||
"logCalls": Array [ | ||
Array [ | ||
"warning", | ||
"📥 - Retrieving schema...", | ||
], | ||
Array [ | ||
"warning", | ||
"📇 - Writing Open API schema...", | ||
], | ||
], | ||
"output": "{ | ||
\\"openapi\\": \\"3.0.2\\", | ||
\\"info\\": { | ||
\\"version\\": \\"0.0.0\\", | ||
\\"title\\": \\"api\\", | ||
\\"description\\": \\"The API\\" | ||
} | ||
}", | ||
"result": undefined, | ||
} | ||
`, | ||
); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
packages/whook-example/src/commands/generateOpenAPISchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { initGetOpenAPI } from '@whook/swagger-ui'; | ||
import { readArgs } from '@whook/cli'; | ||
import { autoService, extra } from 'knifecycle'; | ||
import type { LogService } from 'common-services'; | ||
import type { PromiseValue } from 'type-fest'; | ||
import type { WhookCommandDefinition, WhookCommandArgs } from '@whook/cli'; | ||
|
||
export const definition: WhookCommandDefinition = { | ||
description: 'Write openAPI schema to stdout', | ||
example: `whook generateOpenAPISchema`, | ||
arguments: { | ||
type: 'object', | ||
additionalProperties: false, | ||
required: [], | ||
properties: { | ||
pretty: { | ||
description: 'Option to prettify output', | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
authenticated: { | ||
description: 'Option to get the private routes too', | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default extra(definition, autoService(initGenerateOpenAPISchema)); | ||
|
||
async function initGenerateOpenAPISchema({ | ||
getOpenAPI, | ||
outstream = process.stdout, | ||
args, | ||
log, | ||
}: { | ||
getOpenAPI: PromiseValue<ReturnType<typeof initGetOpenAPI>>; | ||
outstream: NodeJS.WritableStream; | ||
args: WhookCommandArgs; | ||
log: LogService; | ||
}): Promise<() => Promise<void>> { | ||
return async function generateOpenAPISchema() { | ||
const { pretty, authenticated } = readArgs(definition.arguments, args) as { | ||
pretty: boolean; | ||
authenticated: boolean; | ||
}; | ||
|
||
log('warning', '📥 - Retrieving schema...'); | ||
const response = await getOpenAPI({ | ||
authenticated, | ||
mutedMethods: ['options'], | ||
mutedParameters: [], | ||
}); | ||
log('warning', '📇 - Writing Open API schema...'); | ||
|
||
await new Promise((resolve, reject) => { | ||
outstream.once('finish', resolve); | ||
outstream.once('error', reject); | ||
outstream.write( | ||
JSON.stringify(response.body, null, pretty ? 2 : undefined), | ||
); | ||
outstream.end(); | ||
}); | ||
}; | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/whook-example/src/commands/generateOpenAPITypes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import initGenerateOpenAPITypes from './generateOpenAPITypes'; | ||
import { PassThrough } from 'stream'; | ||
import { initGetPingDefinition } from '@whook/whook'; | ||
import type { WhookCommandArgs } from '@whook/cli'; | ||
import type { OpenAPIV3 } from 'openapi-types'; | ||
|
||
describe('generateOpenAPITypes', () => { | ||
const getOpenAPI = jest.fn(); | ||
const log = jest.fn(); | ||
const API: OpenAPIV3.Document = { | ||
openapi: '3.0.2', | ||
info: { | ||
version: '1.0.0', | ||
title: 'Sample OpenAPI', | ||
description: 'A sample OpenAPI file for testing purpose.', | ||
}, | ||
paths: { | ||
[initGetPingDefinition.path]: { | ||
[initGetPingDefinition.method]: initGetPingDefinition.operation, | ||
}, | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
getOpenAPI.mockReset(); | ||
log.mockReset(); | ||
}); | ||
|
||
it('should work', async () => { | ||
const instream = new PassThrough(); | ||
const outstream = new PassThrough(); | ||
const outputPromise = new Promise((resolve, reject) => { | ||
let buffer = Buffer.from(''); | ||
outstream.on('data', (aBuffer) => { | ||
buffer = Buffer.concat([buffer, aBuffer]); | ||
}); | ||
outstream.once('error', () => reject); | ||
outstream.once('end', () => resolve(buffer.toString())); | ||
}); | ||
const generateOpenAPITypes = await initGenerateOpenAPITypes({ | ||
instream, | ||
outstream, | ||
log, | ||
}); | ||
|
||
const resultPromise = generateOpenAPITypes(); | ||
|
||
instream.write(JSON.stringify(API)); | ||
instream.end(); | ||
|
||
expect({ | ||
result: await resultPromise, | ||
output: await outputPromise, | ||
getOpenAPICalls: getOpenAPI.mock.calls, | ||
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), | ||
}).toMatchInlineSnapshot( | ||
{}, | ||
` | ||
Object { | ||
"getOpenAPICalls": Array [], | ||
"logCalls": Array [ | ||
Array [ | ||
"warning", | ||
"📥 - Retrieving API schema...", | ||
], | ||
Array [ | ||
"warning", | ||
"📇 - Writing types...", | ||
], | ||
], | ||
"output": "declare namespace Paths { | ||
namespace GetPing { | ||
namespace Responses { | ||
export interface $200 { | ||
pong?: \\"pong\\"; | ||
} | ||
} | ||
} | ||
} | ||
", | ||
"result": undefined, | ||
} | ||
`, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.