-
-
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.
Merge pull request #76 from nfroidure/feat/dev-reload
feat(dev): watch mode and API types
- Loading branch information
Showing
34 changed files
with
1,001 additions
and
383 deletions.
There are no files selected for viewing
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
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
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 { runServer } from '../src/index'; | ||
const { runServer } = require('../src/index'); | ||
|
||
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(); | ||
}); | ||
}; | ||
} |
Oops, something went wrong.