Skip to content

Commit

Permalink
fix!: make response schema be a map of statuses instead of contents
Browse files Browse the repository at this point in the history
Before

```
 response: {
   content: {
    200: User;
   };
 }
```
After

```
 response: {
   200: {
    content: User;
   };
 }
```
  • Loading branch information
Coobaha committed Feb 19, 2021
1 parent a4ed1d3 commit a8fd0c7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 49 deletions.
10 changes: 8 additions & 2 deletions generator/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import traverse from 'json-schema-traverse';
import mergeAllOf from 'json-schema-merge-allof';
import { JSONSchema7 } from 'json-schema';

const revision = '___v0002'; // + Date.now();
const revision = '__v' + require('../package.json').version; // + Date.now();

function normalizeSchema<T extends JSONSchema7>(originalSchema: T) {
const mergedAllOfSchema = mergeAllOf(originalSchema);
Expand Down Expand Up @@ -140,11 +140,17 @@ export default async (params: { files: string[] }) => {
if (typeof schema.response === 'boolean') continue;
if (typeof schema.response?.properties?.content === 'boolean') continue;

const response = Object.entries(schema.response?.properties ?? {}).reduce((acc, [status, response]) => {
if (typeof response !== 'boolean' && response?.properties?.content) {
acc[status] = response.properties.content;
}
return acc;
}, {} as Record<string, TJS.DefinitionOrBoolean>);
results[key] = {
// @ts-ignore
security: schema.security ? true : undefined,
request: schema.request || {},
response: schema.response?.properties?.content?.properties || {},
response: response,
};
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ export interface Operation {
querystring?: unknown;
};
response?: {
content?: { [Status in StatusCode]?: validResponses } | {};
headers?: OutgoingHttpHeaders & { [HeaderName in string]?: string | string[] | number };
[Status in StatusCode]?: {
description?: string;
content?: validResponses;
headers?: OutgoingHttpHeaders & { [HeaderName in string]?: string | string[] | number };
};
};
}
export interface Schema<SecurityId extends string = string> {
Expand Down
28 changes: 14 additions & 14 deletions src/typed-fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ interface Reply<
P extends keyof ServiceSchema['paths'],
IsKnown = P extends Path ? true : false,
NewOp extends ServiceSchema['paths'][P] = ServiceSchema['paths'][P],
S = keyof Get2<NewOp, 'response', 'content'>,
Content = Get<Get2<NewOp, 'response', 'content'>, S>,
Headers = Get<Get2<NewOp, 'response', 'content'>, S>
S = keyof Get<NewOp, 'response'>,
Content = Get<Get2<NewOp, 'response', S>, 'content'>,
Headers = Get<Get2<NewOp, 'response', S>, 'headers'>
>(
this: any,
path: IsKnown extends true ? P : Path,
Expand All @@ -163,7 +163,7 @@ interface Reply<
: never;

send<
AllHeaders = Get<Op['response'], 'headers'>,
AllHeaders = Get2<Op['response'], Status, 'headers'>,
O = [Headers, AllHeaders],
MissingHeaders = Missing<Headers, AllHeaders>,
MissingStatus = [Status] extends [never] ? true : false
Expand All @@ -177,19 +177,19 @@ interface Reply<
string
>} ]. Please provide required headers before sending reply.`>,
]
: [Get2<Op['response'], 'content', Status>] extends [never]
: [Get2<Op['response'], Status, 'content'>] extends [never]
? []
: [Get2<Op['response'], 'content', Status>]
: [Get2<Op['response'], Status, 'content'>]
): AsReply;

readonly request: Request<ServiceSchema, Op, Path, RawServer, RawRequest>;
readonly statusCode: Status;

headers<Headers extends Get<Op['response'], 'headers'>>(
headers<Headers extends Get2<Op['response'], Status, 'headers'>>(
values: Headers,
): OpaqueReply<Op, Status, Content, Headers, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>;

header<Header extends keyof AllHeaders, AllHeaders = Get<Op['response'], 'headers'>>(
header<Header extends keyof AllHeaders, AllHeaders = Get2<Op['response'], Status, 'headers'>>(
header: Header,
value: AllHeaders[Header],
): OpaqueReply<
Expand All @@ -208,7 +208,7 @@ interface Reply<
getHeader<Header extends keyof Headers>(header: Header): Headers[Header];
getHeaders(): Headers;

redirect<Status extends keyof Get<Op['response'], 'content'>>(
redirect<Status extends keyof Op['response']>(
statusCode: Status,
url: string,
): OpaqueReply<Op, Status, Content, Headers, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>;
Expand All @@ -217,13 +217,13 @@ interface Reply<
): OpaqueReply<Op, 302, Content, Headers, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>;

status<
Status extends [Get<Op['response'], 'content'>] extends [never]
Status extends [keyof Op['response']] extends [never]
? Invalid<` ${Extract<Path, string>} - has no response`>
: keyof Get<Op['response'], 'content'>
: keyof Op['response']
>(
status: Status,
): OpaqueReply<Op, Status, Content, Headers, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>;
code<Status extends keyof Get<Op['response'], 'content'>>(
code<Status extends keyof Op['response']>(
status: Status,
): OpaqueReply<Op, Status, Content, Headers, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>;
}
Expand Down Expand Up @@ -296,10 +296,10 @@ type Handler<
RawRequest extends F.RawRequestDefaultExpression<RawServer> = F.RawRequestDefaultExpression<RawServer>,
RawReply extends F.RawReplyDefaultExpression<RawServer> = F.RawReplyDefaultExpression<RawServer>,
ContextConfig = F.ContextConfigDefault,
ValidSchema = [Get<Op['response'], 'content'>] extends [never]
ValidSchema = [Op['response'][keyof Op['response']]] extends [never]
? Invalid<` ${Extract<Path, string>} - has no response, every path should have at least one response defined`>
: true,
Status extends keyof Get<Op['response'], 'content'> = keyof Get<Op['response'], 'content'>
Status extends keyof Op['response'] = keyof Op['response']
> = ValidSchema extends true
? (
this: F.FastifyInstance<RawServer, RawRequest, RawReply, ContextConfig>,
Expand Down
46 changes: 23 additions & 23 deletions test/better-fastify.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ interface ExampleSchema extends Schema {
};
};
response: {
content: {
200: User;
};
headers: {
'x-custom': string;
200: {
content: User;
headers: {
'x-custom': string;
};
};
};
};
Expand All @@ -52,21 +52,21 @@ interface ExampleSchema extends Schema {
};
};
response: {
content: {
201: User;
};
headers: {
postHeaderRes: string;
postHeaderRes2: string;
201: {
content: User;
headers: {
postHeaderRes: string;
postHeaderRes2: string;
};
};
};
};

'PATCH /': {
request: SharedRequest;
response: {
content: {
204: 'ok';
204: {
content: 'ok';
};
};
};
Expand All @@ -86,16 +86,16 @@ interface ExtendedSchema extends ExampleSchema {
'PATCH /other': {
request: SharedRequest;
response: {
content: {
204: 'ok';
204: {
content: 'ok';
};
};
};
'PATCH /other_empty': {
request: SharedRequest;
response: {
content: {
204: void;
204: {
content: void;
};
};
};
Expand All @@ -106,24 +106,24 @@ interface ExtendedSchema extends ExampleSchema {
};
};
response: {
content: {
200: User;
200: {
content: User;
};
};
};
'GET /test': {
request: GetAndPost;
response: {
content: {
200: 'ok';
200: {
content: 'ok';
};
};
};
'POST /test': {
request: GetAndPost;
response: {
content: {
200: 'ok';
200: {
content: 'ok';
};
};
};
Expand Down
2 changes: 1 addition & 1 deletion test/test_schema.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@
}
}
},
"$hash": "983be0c99c26f024595a2e2434dcf447863e5889193d57b0655ce91b05cd6d01___v0002"
"$hash": "e8c54bf22e2f972ebf0aae46914bb9bed6d83e6e8f1a7de1ea1b18be56ab721b__v0.0.4"
}
14 changes: 7 additions & 7 deletions test/test_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export interface TestSchema extends Schema {
};
};
response: {
content: {
200: User;
};
headers: {
'x-custom': string;
200: {
content: User;
headers: {
'x-custom': string;
};
};
};
};
Expand All @@ -38,8 +38,8 @@ export interface TestSchema extends Schema {
};
};
response: {
content: {
200: { user: User; msg: string };
200: {
content: { user: User; msg: string };
};
};
};
Expand Down

0 comments on commit a8fd0c7

Please sign in to comment.