Add support for path + query variables.

Signed-off-by: Aramis Sennyey <sennyeya@amazon.com>
This commit is contained in:
Aramis Sennyey
2023-03-03 14:43:33 -05:00
committed by Fredrik Adelöw
parent 96f8fe1fa4
commit 056fda5106
6 changed files with 202 additions and 123 deletions
+32 -36
View File
@@ -16,6 +16,30 @@ components:
examples: {}
headers: {}
parameters:
kind:
name: kind
in: path
required: true
schema:
type: string
namespace:
name: namespace
in: path
required: true
schema:
type: string
name:
name: name
in: path
required: true
schema:
type: string
uid:
name: uid
in: path
required: true
schema:
type: string
cursor:
name: cursor
in: query
@@ -634,11 +658,7 @@ paths:
- {}
- JWT: []
parameters:
- in: path
name: uid
required: true
schema:
type: string
- $ref: '#/components/parameters/uid'
delete:
operationId: DeleteEntityByUid
responses:
@@ -667,21 +687,9 @@ paths:
- {}
- JWT: []
parameters:
- in: path
name: kind
required: true
schema:
type: string
- in: path
name: namespace
required: true
schema:
type: string
- in: path
name: name
required: true
schema:
type: string
- $ref: '#/components/parameters/kind'
- $ref: '#/components/parameters/namespace'
- $ref: '#/components/parameters/name'
/entities/by-name/{kind}/{namespace}/{name}/ancestry:
get:
operationId: GetEntityAncestryByName
@@ -696,21 +704,9 @@ paths:
- {}
- JWT: []
parameters:
- in: path
name: kind
required: true
schema:
type: string
- in: path
name: namespace
required: true
schema:
type: string
- in: path
name: name
required: true
schema:
type: string
- $ref: '#/components/parameters/kind'
- $ref: '#/components/parameters/namespace'
- $ref: '#/components/parameters/name'
/entities/by-refs:
post:
operationId: GetEntitiesByRefs
@@ -835,7 +831,7 @@ paths:
name: dryRun
required: false
schema:
type: boolean
type: string
requestBody:
required: true
content:
@@ -35,6 +35,38 @@ export default {
examples: {},
headers: {},
parameters: {
kind: {
name: 'kind',
in: 'path',
required: true,
schema: {
type: 'string',
},
},
namespace: {
name: 'namespace',
in: 'path',
required: true,
schema: {
type: 'string',
},
},
name: {
name: 'name',
in: 'path',
required: true,
schema: {
type: 'string',
},
},
uid: {
name: 'uid',
in: 'path',
required: true,
schema: {
type: 'string',
},
},
cursor: {
name: 'cursor',
in: 'query',
@@ -769,12 +801,7 @@ export default {
],
parameters: [
{
in: 'path',
name: 'uid',
required: true,
schema: {
type: 'string',
},
$ref: '#/components/parameters/uid',
},
],
},
@@ -826,28 +853,13 @@ export default {
],
parameters: [
{
in: 'path',
name: 'kind',
required: true,
schema: {
type: 'string',
},
$ref: '#/components/parameters/kind',
},
{
in: 'path',
name: 'namespace',
required: true,
schema: {
type: 'string',
},
$ref: '#/components/parameters/namespace',
},
{
in: 'path',
name: 'name',
required: true,
schema: {
type: 'string',
},
$ref: '#/components/parameters/name',
},
],
},
@@ -875,28 +887,13 @@ export default {
],
parameters: [
{
in: 'path',
name: 'kind',
required: true,
schema: {
type: 'string',
},
$ref: '#/components/parameters/kind',
},
{
in: 'path',
name: 'namespace',
required: true,
schema: {
type: 'string',
},
$ref: '#/components/parameters/namespace',
},
{
in: 'path',
name: 'name',
required: true,
schema: {
type: 'string',
},
$ref: '#/components/parameters/name',
},
],
},
@@ -1104,7 +1101,7 @@ export default {
name: 'dryRun',
required: false,
schema: {
type: 'boolean',
type: 'string',
},
},
],
@@ -230,3 +230,36 @@ export type UnknownIfNever<P> = [P] extends [never] ? unknown : P;
* @public
*/
export type ToTypeSafe<T> = UnknownIfNever<ConvertAll<TuplifyUnion<T>>[number]>;
type DiscriminateUnion<T, K extends keyof T, V extends T[K]> = Extract<
T,
Record<K, V>
>;
export type MapDiscriminatedUnion<
T extends Record<K, string>,
K extends keyof T,
> = {
[V in T[K]]: DiscriminateUnion<T, K, V>;
};
export type PickOptionalKeys<T extends { [key: string]: any }> = {
[K in keyof T]: true extends T[K]['required'] ? never : K;
}[keyof T];
export type PickRequiredKeys<T extends { [key: string]: any }> = {
[K in keyof T]: true extends T[K]['required'] ? K : never;
}[keyof T];
export type OptionalMap<T extends { [key: string]: any }> = {
[P in Exclude<PickOptionalKeys<T>, undefined>]?: NonNullable<T[P]>;
};
export type RequiredMap<T extends { [key: string]: any }> = {
[P in Exclude<PickRequiredKeys<T>, undefined>]: NonNullable<T[P]>;
};
export type FullMap<T extends { [key: string]: any }> = RequiredMap<T> &
OptionalMap<T>;
export type Filter<T, U> = T extends U ? T : never;
+5 -4
View File
@@ -15,6 +15,7 @@
*/
import core from 'express-serve-static-core';
import { DocPathTemplate, MethodAwareDocPath, RequiredDoc } from './common';
import { PathSchema, QuerySchema } from './params';
import { RequestBodyToJsonSchema } from './requests';
import { ResponseBodyToJsonSchema } from './responses';
@@ -35,10 +36,10 @@ export type DocRequestHandler<
Path extends DocPathTemplate<Doc>,
Method extends keyof Doc['paths'][Path],
> = core.RequestHandler<
core.ParamsDictionary,
PathSchema<Doc, Path, Method>,
ResponseBodyToJsonSchema<Doc, Path, Method>,
RequestBodyToJsonSchema<Doc, Path, Method>,
ParsedQs,
QuerySchema<Doc, Path, Method>,
Record<string, string>
>;
@@ -51,10 +52,10 @@ export type DocRequestHandlerParams<
Path extends DocPathTemplate<Doc>,
Method extends keyof Doc['paths'][Path],
> = core.RequestHandlerParams<
core.ParamsDictionary,
PathSchema<Doc, Path, Method>,
ResponseBodyToJsonSchema<Doc, Path, Method>,
RequestBodyToJsonSchema<Doc, Path, Method>,
ParsedQs,
QuerySchema<Doc, Path, Method>,
Record<string, string>
>;
@@ -19,6 +19,7 @@ import type {
ReferenceObject,
RequestBodyObject,
ParameterObject,
SchemaObject,
} from 'openapi3-ts';
/**
@@ -81,21 +82,23 @@ export type ImmutableHeaderObject = ImmutableObject<HeaderObject>;
export interface CookieObject extends ParameterObject {
in: 'cookie';
style: 'form';
style?: 'form';
}
export type ImmutableCookieObject = ImmutableObject<CookieObject>;
export interface QueryObject extends ParameterObject {
in: 'query';
style: 'form' | 'deepObject' | 'pipeDelimited' | 'spaceDelimited';
style?: 'form' | 'deepObject' | 'pipeDelimited' | 'spaceDelimited';
}
export type ImmutableQueryObject = ImmutableObject<QueryObject>;
export interface PathObject extends ParameterObject {
in: 'path';
style: 'simple' | 'label' | 'matrix';
style?: 'simple' | 'label' | 'matrix';
}
export type ImmutablePathObject = ImmutableObject<PathObject>;
export type ImmutableSchemaObject = ImmutableObject<SchemaObject>;
+86 -37
View File
@@ -15,23 +15,30 @@
*/
import {
ImmutableCookieObject,
ImmutableHeaderObject,
ImmutableParameterObject,
ImmutablePathObject,
ImmutableQueryObject,
ImmutableReferenceObject,
ImmutableSchemaObject,
} from './immutable';
import {
ComponentRef,
ComponentTypes,
DocOperation,
DocPathTemplate,
DocPath,
DocPathMethod,
Filter,
FullMap,
MapDiscriminatedUnion,
PathTemplate,
RequiredDoc,
TuplifyUnion,
} from './common';
import { FromSchema, JSONSchema7 } from 'json-schema-to-ts';
import spec from '../schema/petstore';
import { ParameterLocation } from 'openapi3-ts';
export type ResolveDocParameter<
export type DocParameter<
Doc extends RequiredDoc,
Path extends Extract<keyof Doc['paths'], string>,
Method extends keyof Doc['paths'][Path],
@@ -45,52 +52,94 @@ export type ResolveDocParameter<
? ComponentRef<
Doc,
'parameters',
DocOperation<Doc, Path, Method>['parameter'][Parameter]
DocOperation<Doc, Path, Method>['parameters'][Parameter]
>
: never
: DocOperation<Doc, Path, Method>['parameters'][Parameter];
export type DocParameter<
Doc extends RequiredDoc,
Path extends Extract<keyof Doc['paths'], string>,
Method extends keyof Doc['paths'][Path],
Parameter extends keyof Doc['paths'][Path][Method]['parameters'],
> = ResolveDocParameter<
Doc,
Path,
Method,
Parameter
> extends ImmutableParameterObject
? ResolveDocParameter<Doc, Path, Method, Parameter>
: never;
type KeysMatching<T extends any[], V> = {
[K in keyof T]-?: T[K] extends V ? K : never;
}[keyof T];
/**
* @public
*/
export type ConvertAll<T, R extends ReadonlyArray<unknown> = []> = T extends [
infer First extends JSONSchema7,
...infer Rest,
]
? ConvertAll<Rest, [...R, FromSchema<First>]>
: R;
/**
* @public
*/
export type DocParameters<
Doc extends RequiredDoc,
Path extends DocPathTemplate<Doc>,
Path extends Extract<keyof Doc['paths'], string>,
Method extends keyof Doc['paths'][Path],
> = TuplifyUnion<>;
> = DocOperation<Doc, Path, Method>['parameters'] extends ReadonlyArray<any>
? {
[Index in keyof DocOperation<
Doc,
Path,
Method
>['parameters']]: DocParameter<Doc, Path, Method, Index>;
}
: never;
export type ResolveDocParameterSchema<
Doc extends RequiredDoc,
Schema extends ImmutableParameterObject['schema'],
> = Schema extends ImmutableReferenceObject
? 'parameters' extends ComponentTypes<Doc>
? ComponentRef<Doc, 'parameters', Schema>
: never
: Schema;
export type ParameterSchema<
Doc extends RequiredDoc,
Schema extends ImmutableParameterObject['schema'],
> = ResolveDocParameterSchema<Doc, Schema> extends infer R
? R extends ImmutableSchemaObject
? R extends JSONSchema7
? FromSchema<R>
: never
: never
: never;
type MapToSchema<
Doc extends RequiredDoc,
T extends Record<string, ImmutableParameterObject>,
> = {
[V in keyof T]: NonNullable<T[V]> extends ImmutableParameterObject
? ParameterSchema<Doc, NonNullable<T[V]>['schema']>
: never;
};
export type ParametersSchema<
Doc extends RequiredDoc,
Path extends Extract<keyof Doc['paths'], string>,
Method extends keyof Doc['paths'][Path],
FilterType extends ImmutableParameterObject,
> = number extends keyof DocParameters<Doc, Path, Method>
? MapToSchema<
Doc,
FullMap<
MapDiscriminatedUnion<
Filter<DocParameters<Doc, Path, Method>[number], FilterType>,
'name'
>
>
>
: never;
export type HeaderSchema<
Doc extends RequiredDoc,
Path extends PathTemplate<Extract<keyof Doc['paths'], string>>,
Method extends keyof Doc['paths'][Path],
> = KeysMatching<DocParameters<Doc, Path, Method>, ImmutableHeaderObject>;
Method extends DocPathMethod<Doc, Path>,
> = ParametersSchema<Doc, DocPath<Doc, Path>, Method, ImmutableHeaderObject>;
type keys = HeaderSchema<typeof spec, '/pets', 'get'>;
export type CookieSchema<
Doc extends RequiredDoc,
Path extends PathTemplate<Extract<keyof Doc['paths'], string>>,
Method extends DocPathMethod<Doc, Path>,
> = ParametersSchema<Doc, DocPath<Doc, Path>, Method, ImmutableCookieObject>;
type match = KeysMatching<[1, 2, 3], 1>;
export type PathSchema<
Doc extends RequiredDoc,
Path extends PathTemplate<Extract<keyof Doc['paths'], string>>,
Method extends DocPathMethod<Doc, Path>,
> = ParametersSchema<Doc, DocPath<Doc, Path>, Method, ImmutablePathObject>;
export type QuerySchema<
Doc extends RequiredDoc,
Path extends PathTemplate<Extract<keyof Doc['paths'], string>>,
Method extends DocPathMethod<Doc, Path>,
> = ParametersSchema<Doc, DocPath<Doc, Path>, Method, ImmutableQueryObject>;