simonireilly / compeller

A strong typescript binding for your OpenAPI Schema that doesn't need generation and is not prescriptive in coding style
MIT License
22 stars 1 forks source link

feat: create interface for responder so responder types take presidence over default types #27

Closed simonireilly closed 2 years ago

simonireilly commented 2 years ago

This first feature abstracts responders so that the response type can be composed functionally.

The benefit of this is that we can retain the typed interface:

response(statusCode, body)

At this time the body is permitted to be any type, while statusCode must extend string, symbol, or number.

Composing a responder can be done functionally:

const customerCompeller = compeller(OpenAPISpecification, {
  responder: (statusCode, body) => {
    return typeof statusCode === 'string'
      ? {
          statusCode: parseInt(statusCode),
          body: JSON.stringify(body),
        }
      : {
          statusCode,
          body: JSON.stringify(body),
        };
  },
});

Enabling injection.