Skip to content

Commit

Permalink
chore: add type checked testing to ensure we do not have regressions …
Browse files Browse the repository at this point in the history
…- coverage via integration tests (#28)
  • Loading branch information
simonireilly authored Jan 28, 2022
1 parent 65a8af0 commit 8234a8f
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:

- run: yarn

- run: yarn typecheck

- run: yarn test

publish-gpr:
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ __tests__
assets
examples
src
!dist/src
tmp

# Ignored file types
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const OpenAPISpecification = {
},
openapi: '3.1.0',
paths: {
'v1//version': {
'v1/version': {
get: {
responses: {
'200': {
Expand All @@ -78,6 +78,7 @@ export const OpenAPISpecification = {
},
},
};

```

With compeller you can compile this into a typed request and response handler like:
Expand Down
29 changes: 28 additions & 1 deletion __tests__/integration/aws/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
import { handler } from '../fixtures';
import { OpenAPISpecification } from '../fixtures/openapi/spec';
import { APIGatewayV1Responder, compeller } from '../../../src';

const API = compeller(OpenAPISpecification, {
responder: APIGatewayV1Responder,
});
const { response, request } = API('/pets', 'post');

export const handler = (data: Record<string, unknown>) => {
let body = data;

if (request.validator(body)) {
console.info('Type-safe object destructured from post request', {
name: body.name,
});

return response('201', {});
} else {
const { errors } = request.validator;

if (errors) {
return response('422', {
title: 'Schema validation failure for request body',
details: errors,
});
}
}
};

describe('integration tests', () => {
it('with missing data', () => {
Expand Down
22 changes: 0 additions & 22 deletions __tests__/integration/fixtures/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions examples/openapi/api-gateway.ts → examples/api-gateway.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIGatewayV1Responder, compeller } from '../../src';
import { OpenAPISpecification } from './spec';
import { APIGatewayV1Responder, compeller } from '../src';
import { OpenAPISpecification } from './openapi/spec';

const apiGatewayV1Compeller = compeller(OpenAPISpecification, {
responder: APIGatewayV1Responder,
Expand Down
4 changes: 2 additions & 2 deletions examples/openapi/custom.ts → examples/custom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { compeller } from '../../src';
import { OpenAPISpecification } from './spec';
import { compeller } from '../src';
import { OpenAPISpecification } from './openapi/spec';

const customerCompeller = compeller(OpenAPISpecification, {
responder: (statusCode, body) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/openapi/default.ts → examples/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { compeller } from '../../src';
import { OpenAPISpecification } from './spec';
import { compeller } from '../src';
import { OpenAPISpecification } from './openapi/spec';

const defaultCompeller = compeller(OpenAPISpecification);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"license": "ISC",
"main": "dist/index.js",
"scripts": {
"build": "tsc --build",
"typecheck": "tsc",
"build": "tsc --project tsconfig.production.json",
"clean": "rimraf ./dist",
"embed": "embedme ./README.md",
"example": "hygen compeller new --directory example",
Expand Down
14 changes: 8 additions & 6 deletions src/compeller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,19 @@ export const compeller = <
} = {},
} = spec.paths[path][method];

const unsafeSchema = schema as JSONSchemaType<FromSchema<SC>>;
// TODO: We need to handle the request not a requestBody
//
// Some users might abstract the functional components into a generic
// wrapper, therefore gets might hit the validator path
//
// We don't want to loose type safety
const unsafeSchema = (schema || {}) as JSONSchemaType<FromSchema<SC>>;

const ajv = new Ajv({
allErrors: true,
});

if (unsafeSchema) {
return ajv.compile(unsafeSchema);
} else {
return ajv.compile({});
}
return ajv.compile<FromSchema<SC>>(unsafeSchema);
};

const validator = validateRequestBody();
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@tsconfig/node14",
"include": ["src/**/*.ts"],
"include": ["src/**/*.ts", "__tests__/**/*.ts"],
"compilerOptions": {
"outDir": "./dist",
"declaration": true,
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["__tests__/**/*.ts"]
}

0 comments on commit 8234a8f

Please sign in to comment.