Skip to content

Commit 0ed021e

Browse files
committed
Start processing path and operations
1 parent 8b889c2 commit 0ed021e

File tree

6 files changed

+165
-3
lines changed

6 files changed

+165
-3
lines changed

src/compileOperation.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { OpenAPIOperation } from "./types";
55
import { compileValueSchema } from './compileValueSchema';
66
import { ValidationErrorIdentifier } from './error';
77

8-
98
/**
109
* Compile an operation into a function.
1110
* The value input is an object:
@@ -89,7 +88,6 @@ export function compileOperation(compiler: Compiler, operation: OpenAPIOperation
8988
])
9089
));
9190
}
92-
9391
} else {
9492
nodes.push(builders.ifStatement(
9593
builders.binaryExpression(

src/compilePath.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { namedTypes, builders } from 'ast-types';
2+
3+
import { Compiler } from "./compiler";
4+
import { OpenAPIPath } from "./types";
5+
import { compileValueSchema } from './compileValueSchema';
6+
import { ValidationErrorIdentifier } from './error';
7+
import { compileOperation } from './compileOperation';
8+
9+
/**
10+
* Compile a path into a function.
11+
* The value input is an object:
12+
* {
13+
* path: string;
14+
* method: string;
15+
* body: any;
16+
* query: any;
17+
* headers: any;
18+
* }
19+
*/
20+
export function compilePath(compiler: Compiler, pathOperations: OpenAPIPath) {
21+
return compiler.defineValidationFunction(pathOperations, ({ value, path, error }) => {
22+
const nodes: namedTypes.BlockStatement['body'] = [];
23+
24+
Object.entries(pathOperations).forEach(([method, operation]) => {
25+
const fnOperation = compileOperation(compiler, operation);
26+
27+
nodes.push(
28+
builders.ifStatement(
29+
builders.binaryExpression(
30+
'===',
31+
builders.memberExpression(
32+
value,
33+
builders.identifier('method'),
34+
),
35+
builders.literal(method),
36+
),
37+
builders.blockStatement([
38+
builders.returnStatement(
39+
builders.callExpression(fnOperation, [
40+
path,
41+
value,
42+
]),
43+
),
44+
]),
45+
)
46+
)
47+
});
48+
49+
nodes.push(
50+
builders.returnStatement(
51+
error('method not supported')
52+
),
53+
)
54+
55+
return nodes;
56+
});
57+
}

src/hash.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const PRESERVE_PROPS = [
2323
'additionalProperties',
2424
'minProperties',
2525
'maxProperties',
26+
'requestBody',
27+
'get',
28+
'post',
29+
'put',
30+
'delete',
31+
'patch',
2632
]
2733

2834
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Bun Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`with get and post 1`] = `
4+
"class ValidationError {
5+
constructor(path, message) {
6+
super(message);
7+
this.path = path;
8+
}
9+
}
10+
function obj1(path, value) {
11+
if (value.body !== undefined) {
12+
return new ValidationError(path, 'body is not allowed');
13+
}
14+
return value;
15+
}
16+
function obj4(path, value) {
17+
if (!(typeof value === 'number')) {
18+
return new ValidationError(path, 'Expected a number');
19+
}
20+
return value;
21+
}
22+
function obj3(path, value) {
23+
const keys = new Set(Object.keys(value));
24+
const value0 = value['foo'];
25+
if (value0 !== undefined) {
26+
const result0 = obj4([
27+
...path,
28+
'foo'
29+
], value0);
30+
if (result0 instanceof ValidationError) {
31+
return result0;
32+
}
33+
value['foo'] = result0;
34+
}
35+
if (keys.size > 0) {
36+
return new ValidationError(path, 'Unexpected properties');
37+
}
38+
return value;
39+
}
40+
function obj2(path, value) {
41+
if (value.body === undefined) {
42+
return new ValidationError(path, 'body is required');
43+
}
44+
const body = obj3([
45+
...path,
46+
'body'
47+
], value.body);
48+
if (body instanceof ValidationError) {
49+
return body;
50+
} else {
51+
value.body = body;
52+
}
53+
return value;
54+
}
55+
function obj0(path, value) {
56+
if (value.method === 'get') {
57+
return obj1(path, value);
58+
}
59+
if (value.method === 'post') {
60+
return obj2(path, value);
61+
}
62+
return new ValidationError(path, 'method not supported');
63+
}"
64+
`;

src/tests/compileOperation.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ describe('With body', () => {
2727
},
2828
}
2929
});
30-
console.log(compiler.compile());
3130
expect(compiler.compile()).toMatchSnapshot();
3231
});
3332
});

src/tests/compilePath.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Compiler } from '../compiler';
2+
import { compilePath } from '../compilePath';
3+
4+
test('with get and post', () => {
5+
const compiler = new Compiler();
6+
compilePath(compiler, {
7+
get: {
8+
parameters: [
9+
{
10+
name: 'foo',
11+
in: 'query',
12+
schema: {
13+
type: 'number',
14+
},
15+
},
16+
]
17+
},
18+
post: {
19+
requestBody: {
20+
required: true,
21+
content: {
22+
'application/json': {
23+
schema: {
24+
type: 'object',
25+
properties: {
26+
foo: {
27+
type: 'number',
28+
},
29+
},
30+
},
31+
},
32+
},
33+
}
34+
}
35+
});
36+
console.log(compiler.compile());
37+
expect(compiler.compile()).toMatchSnapshot();
38+
});

0 commit comments

Comments
 (0)