Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/setup npm publishing #2

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules

dist

*.tgz
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Compel
# Compeller

A strong typescript binding for your OpenAPI Schema that doesn't need generation.

- [Compel](#compel)
- [Compeller](#compeller)
- [About](#about)
- [Shoulders](#shoulders)

## About

Compel tries to infer your OpenAPI validations and responses, from a typed OpenAPI specification.
Compeller tries to infer your OpenAPI validations and responses, from a typed OpenAPI specification.

Say you had the following specification:

Expand Down Expand Up @@ -52,7 +52,7 @@ With compel you can compile this into a typed request and response handler like:

```ts
import {openAPISpec} from './spec';
const stuff = APICompiler(spec);
const stuff = compeller(spec);

const { response } = stuff('/test', 'get');

Expand Down
5 changes: 2 additions & 3 deletions __tests__/compiler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
import { APICompiler } from './../lib/compiler';
import { compeller } from '../src';

const spec = {
info: {
Expand Down Expand Up @@ -37,7 +36,7 @@ const spec = {
describe('API Compiler tests', () => {
describe('get requests', () => {
it('requires a valid API document', () => {
const stuff = APICompiler(spec);
const stuff = compeller(spec);

const { response } = stuff('/test', 'get');

Expand Down
4 changes: 2 additions & 2 deletions __tests__/integration/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAPISpecification } from './openapi/spec';
import { APICompiler } from '../../../lib/compiler';
import { compeller } from '../../../src';

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

export const handler = (data: Record<string, unknown>) => {
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
{
"name": "combined",
"name": "compeller",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "jest",
"prepare": "husky install"
"build": "tsc --build",
"clean": "tsc --build --clean",
"prepare": "husky install",
"prepack": "yarn clean && yarn build"
},
"keywords": [],
"author": "simon",
"files": [
"dist/**/*",
"README.md"
],
"license": "ISC",
"dependencies": {
"ajv": "^8.9.0",
Expand Down
17 changes: 10 additions & 7 deletions lib/compiler.ts → src/compeller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { OpenAPIObject } from 'openapi3-ts';
* @default 'application/json'
* @returns
*/
export const APICompiler = <
export const compeller = <
T extends OpenAPIObject,
U extends string = 'application/json'
>(
Expand Down Expand Up @@ -45,17 +45,20 @@ export const APICompiler = <
const validateRequestBody = <
SC extends T['paths'][P][M]['requestBody']['content'][U]['schema']
>() => {
const { requestBody } = spec.paths[path][method];
const {
requestBody: {
content: { [contentType]: { schema = undefined } = {} } = {},
} = {},
} = spec.paths[path][method];

const unsafeSchema = schema as JSONSchemaType<FromSchema<SC>>;

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

if (requestBody) {
const schema = requestBody.content[contentType]
.schema as JSONSchemaType<FromSchema<SC>>;

return ajv.compile(schema);
if (unsafeSchema) {
return ajv.compile(unsafeSchema);
} else {
return ajv.compile({});
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { compeller } from './compeller';
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": "@tsconfig/node14"
"extends": "@tsconfig/node14",
"include": ["src/**/*.ts"],
"compilerOptions": {
"outDir": "./dist",
"declaration": true
}
}