Skip to content

Commit

Permalink
Remove all usage of unsafe casts (outside of tests) (#108)
Browse files Browse the repository at this point in the history
This looks like a big diff because of Prettier, but all it's doing is changing `a.reduce(..., {} as T)` to `a.reduce<T>(..., {})` 😄
  • Loading branch information
fwouts authored Feb 21, 2019
1 parent f1e897b commit 9634463
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 78 deletions.
55 changes: 26 additions & 29 deletions lib/src/generators/contract/openapi2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,32 @@ export function openApiV2(contractDefinition: ContractDefinition): OpenApiV2 {
name: "TODO"
}
},
paths: contractDefinition.endpoints.reduce(
(acc, endpoint) => {
const openApiPath = endpoint.path.replace(/:(\w+)/g, "{$1}");
acc[openApiPath] = acc[openApiPath] || {};
acc[openApiPath][endpoint.method.toLowerCase()] = {
operationId: endpoint.name,
description: endpoint.description,
tags: endpoint.tags,
parameters: getParameters(endpoint),
responses: {
...(endpoint.defaultResponse
? { default: response(endpoint.defaultResponse) }
: {}),
...endpoint.responses.reduce<{
[statusCode: string]: OpenAPIV2Response;
}>((acc, responseNode) => {
acc[responseNode.status.toString(10)] = response(responseNode);
return acc;
}, {})
}
};
return acc;
},
{} as {
[endpointPath: string]: {
[method: string]: OpenAPIV2Operation;
};
}
),
paths: contractDefinition.endpoints.reduce<{
[endpointPath: string]: {
[method: string]: OpenAPIV2Operation;
};
}>((acc, endpoint) => {
const openApiPath = endpoint.path.replace(/:(\w+)/g, "{$1}");
acc[openApiPath] = acc[openApiPath] || {};
acc[openApiPath][endpoint.method.toLowerCase()] = {
operationId: endpoint.name,
description: endpoint.description,
tags: endpoint.tags,
parameters: getParameters(endpoint),
responses: {
...(endpoint.defaultResponse
? { default: response(endpoint.defaultResponse) }
: {}),
...endpoint.responses.reduce<{
[statusCode: string]: OpenAPIV2Response;
}>((acc, responseNode) => {
acc[responseNode.status.toString(10)] = response(responseNode);
return acc;
}, {})
}
};
return acc;
}, {}),
definitions: contractDefinition.types.reduce<{
[typeName: string]: OpenAPI2SchemaType;
}>((acc, typeNode) => {
Expand Down
95 changes: 46 additions & 49 deletions lib/src/generators/contract/openapi3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,56 +49,53 @@ export function openApiV3(contractDefinition: ContractDefinition): OpenApiV3 {
]
}
: {}),
paths: contractDefinition.endpoints.reduce(
(acc, endpoint) => {
const openApiPath = endpoint.path.replace(/:(\w+)/g, "{$1}");
acc[openApiPath] = acc[openApiPath] || {};
acc[openApiPath][endpoint.method.toLowerCase()] = {
operationId: endpoint.name,
description: endpoint.description,
tags: endpoint.tags,
parameters: getParameters(contractDefinition.types, endpoint),
...(endpoint.request.body && {
requestBody: {
content: {
"application/json": {
schema: openApi3TypeSchema(
contractDefinition.types,
endpoint.request.body.type
)
}
},
description: endpoint.description || ""
}
}),
responses: {
...(endpoint.defaultResponse
? {
default: response(
contractDefinition.types,
endpoint.defaultResponse
)
}
: {}),
...endpoint.responses.reduce<{
[statusCode: string]: OpenAPIV3Body;
}>((acc, responseNode) => {
acc[responseNode.status.toString(10)] = response(
contractDefinition.types,
responseNode
);
return acc;
}, {})
paths: contractDefinition.endpoints.reduce<{
[endpointPath: string]: {
[method: string]: OpenAPIV3Operation;
};
}>((acc, endpoint) => {
const openApiPath = endpoint.path.replace(/:(\w+)/g, "{$1}");
acc[openApiPath] = acc[openApiPath] || {};
acc[openApiPath][endpoint.method.toLowerCase()] = {
operationId: endpoint.name,
description: endpoint.description,
tags: endpoint.tags,
parameters: getParameters(contractDefinition.types, endpoint),
...(endpoint.request.body && {
requestBody: {
content: {
"application/json": {
schema: openApi3TypeSchema(
contractDefinition.types,
endpoint.request.body.type
)
}
},
description: endpoint.description || ""
}
};
return acc;
},
{} as {
[endpointPath: string]: {
[method: string]: OpenAPIV3Operation;
};
}
),
}),
responses: {
...(endpoint.defaultResponse
? {
default: response(
contractDefinition.types,
endpoint.defaultResponse
)
}
: {}),
...endpoint.responses.reduce<{
[statusCode: string]: OpenAPIV3Body;
}>((acc, responseNode) => {
acc[responseNode.status.toString(10)] = response(
contractDefinition.types,
responseNode
);
return acc;
}, {})
}
};
return acc;
}, {}),
components: {
schemas: contractDefinition.types.reduce<{
[typeName: string]: OpenAPI3SchemaType;
Expand Down

0 comments on commit 9634463

Please sign in to comment.