-
Notifications
You must be signed in to change notification settings - Fork 2
Expose a programatic API #267
Comments
I could even write a programmatic version of ncdc. You'd pass it some io-ts (or similar) functions rather than just a type name. That has a couple benefits
Mysteries:
|
Or even something like this. Lets say a user has a interface Book {
ISBN: string
ISBN_13: string
author: string
title: string
inventoryId: string
}
const isBook = (responseBody: unknown): asserts responseBody is Book => {
if (typeof responseBody !== 'object') {
throw new Error('Expected val to be of type string')
}
// more validation logic would go here
}
const config = {
services: {
bookService: {
port: 3000,
realUrl: 'https://example.com',
tsconfig: './path/to/tsconfig.json',
rateLimit: 300,
resources: [
{
name: 'Book not found',
serveOnly: true,
request: {
method: 'GET',
endpoints: '/api/books/a-bad-id',
},
response: {
code: 404,
},
},
{
name: 'Book',
request: {
method: 'GET',
endpoints: ['/api/books/123', '/api/books/456'],
serveEndpoint: '/api/books/:id',
},
response: {
code: 200,
serveBody: {
ISBN: '9780141187761',
ISBN_13: '978-0141187761',
author: 'George Orwell',
title: ' 1984 Nineteen Eighty-Four',
inventoryId: 'item-87623',
},
// validator would replace the old "type" property
validator: isBook,
},
},
],
},
},
}
export default config
/**
* the new usage of the CLI would be:
* ncdc serve blah.ts --watch
* ncdc test blah.ts
*
* maybe there'd also be an option to choose the particular services, e.g
* ncdc serve blah.ts filmService --watch
* ncdc test blah.ts bookService
*/ Things this would change
Things that stay the same
|
BREAKING CHANGE: Deleted the generate command and related code re #267
Taking a different, simpler approach: Rather than using some serious magic, with an ncdc.ts, people can just write their own script that create an NCDC instance which exposes methods to create/test/generate etc. Here's the same Books service example from the docs again, but also with a Films service that would be hosted on port 4001. #!/usr/bin/env -S npx ts-node -P ./scripts/tsconfig.json ./scripts/fakes.ts
/* eslint-disable @typescript-eslint/no-var-requires */
import { resolve } from 'path'
import { NCDC, Method } from 'ncdc'
async function start(): Promise<void> {
const ncdc = new NCDC(
[
{
name: 'Books service',
port: 4000,
baseUrl: 'https://example.com',
resources: [
{
name: 'Book not found',
serveOnly: true,
request: {
method: Method.GET,
endpoints: ['/api/books/a-bad-id'],
},
response: {
code: 404,
},
},
{
name: 'Book',
serveOnly: false,
request: {
method: Method.GET,
body: undefined,
endpoints: ['/api/books/123', '/api/books/456'],
serveEndpoint: '/api/books/*',
},
response: {
code: 200,
headers: { 'content-type': 'application/json' },
type: 'Book',
serveBody: {
ISBN: '9780141187761',
ISBN_13: '978-0141187761',
author: 'George Orwell',
title: '1984 Nineteen Eighty-Four',
inventoryId: 'bitem-87623',
},
},
},
],
},
{
name: 'Films service',
port: 4001,
baseUrl: 'https://example.com',
resources: [
{
name: 'Serve error',
serveOnly: false,
request: {
endpoints: [],
serveEndpoint: '/api/*',
method: Method.GET,
},
response: {
code: 401,
body: 'nice meme, lol',
type: 'string',
},
},
],
},
],
{ tsconfigPath: getPath('./tsconfig.json') },
)
if (process.argv.includes('--serve')) {
await ncdc.serve({ watch: true })
} else if (process.argv.includes('--test')) {
await ncdc.test({})
} else if (process.argv.includes('--generate')) {
await ncdc.generate({ force: false, outputPath: getPath('json-schema') })
} else {
// a sensible-ish default if no flag is provided
await ncdc.serve({ watch: true })
}
return
}
void start().catch((err) => {
console.log('fatal error', err)
process.exit(1)
})
function getPath(pathRelativeToRepoRoot: string) {
return resolve(process.cwd(), pathRelativeToRepoRoot)
} The string Things this would change
Things that stay the same
|
It'd be cool if JS could be evaluated to create fixtures from the export of a javascript file.
The text was updated successfully, but these errors were encountered: