Testing for parameter typings.
Signed-off-by: Aramis Sennyey <sennyeya@amazon.com>
This commit is contained in:
committed by
Fredrik Adelöw
parent
2cc50f71c4
commit
96f8fe1fa4
+1
-1
@@ -1,4 +1,4 @@
|
||||
#!/bin/sh
|
||||
#!/bin/zsh
|
||||
. "$(dirname "$0")/_/husky.sh"
|
||||
|
||||
yarn lint-staged
|
||||
|
||||
@@ -24,7 +24,8 @@
|
||||
"postpack": "backstage-cli package postpack"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "workspace:^"
|
||||
"@backstage/cli": "workspace:^",
|
||||
"expect-type": "^0.15.0"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
|
||||
@@ -18,6 +18,7 @@ import type {
|
||||
OpenAPIObject,
|
||||
ReferenceObject,
|
||||
RequestBodyObject,
|
||||
ParameterObject,
|
||||
} from 'openapi3-ts';
|
||||
|
||||
/**
|
||||
@@ -68,3 +69,33 @@ export type ImmutableContentObject = ImmutableObject<ContentObject>;
|
||||
* @public
|
||||
*/
|
||||
export type ImmutableRequestBodyObject = ImmutableObject<RequestBodyObject>;
|
||||
|
||||
export type ImmutableParameterObject = ImmutableObject<ParameterObject>;
|
||||
|
||||
export interface HeaderObject extends ParameterObject {
|
||||
in: 'header';
|
||||
style: 'simple';
|
||||
}
|
||||
|
||||
export type ImmutableHeaderObject = ImmutableObject<HeaderObject>;
|
||||
|
||||
export interface CookieObject extends ParameterObject {
|
||||
in: 'cookie';
|
||||
style: 'form';
|
||||
}
|
||||
|
||||
export type ImmutableCookieObject = ImmutableObject<CookieObject>;
|
||||
|
||||
export interface QueryObject extends ParameterObject {
|
||||
in: 'query';
|
||||
style: 'form' | 'deepObject' | 'pipeDelimited' | 'spaceDelimited';
|
||||
}
|
||||
|
||||
export type ImmutableQueryObject = ImmutableObject<QueryObject>;
|
||||
|
||||
export interface PathObject extends ParameterObject {
|
||||
in: 'path';
|
||||
style: 'simple' | 'label' | 'matrix';
|
||||
}
|
||||
|
||||
export type ImmutablePathObject = ImmutableObject<PathObject>;
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
ImmutableHeaderObject,
|
||||
ImmutableParameterObject,
|
||||
ImmutableReferenceObject,
|
||||
} from './immutable';
|
||||
import {
|
||||
ComponentRef,
|
||||
ComponentTypes,
|
||||
DocOperation,
|
||||
DocPathTemplate,
|
||||
PathTemplate,
|
||||
RequiredDoc,
|
||||
TuplifyUnion,
|
||||
} from './common';
|
||||
import spec from '../schema/petstore';
|
||||
import { ParameterLocation } from 'openapi3-ts';
|
||||
|
||||
export type ResolveDocParameter<
|
||||
Doc extends RequiredDoc,
|
||||
Path extends Extract<keyof Doc['paths'], string>,
|
||||
Method extends keyof Doc['paths'][Path],
|
||||
Parameter extends keyof Doc['paths'][Path][Method]['parameters'],
|
||||
> = DocOperation<
|
||||
Doc,
|
||||
Path,
|
||||
Method
|
||||
>['parameters'][Parameter] extends ImmutableReferenceObject
|
||||
? 'parameters' extends ComponentTypes<Doc>
|
||||
? ComponentRef<
|
||||
Doc,
|
||||
'parameters',
|
||||
DocOperation<Doc, Path, Method>['parameter'][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>,
|
||||
Method extends keyof Doc['paths'][Path],
|
||||
> = TuplifyUnion<>;
|
||||
|
||||
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>;
|
||||
|
||||
type keys = HeaderSchema<typeof spec, '/pets', 'get'>;
|
||||
|
||||
type match = KeysMatching<[1, 2, 3], 1>;
|
||||
@@ -7802,6 +7802,7 @@ __metadata:
|
||||
"@backstage/cli": "workspace:^"
|
||||
"@types/express": ^4.17.6
|
||||
"@types/express-serve-static-core": ^4.17.33
|
||||
expect-type: ^0.15.0
|
||||
express: ^4.17.1
|
||||
express-promise-router: ^4.1.0
|
||||
json-schema-to-ts: ^2.6.2
|
||||
@@ -24090,6 +24091,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"expect-type@npm:^0.15.0":
|
||||
version: 0.15.0
|
||||
resolution: "expect-type@npm:0.15.0"
|
||||
checksum: f5d3733bb593d9f7cf302b393e34b19389fb7022d72963815e9a847a9517b65201faef733dd9006bed9e200486d77204fd958c8d29ef58b466bed2da076fc0ba
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"expect@npm:^28.0.0":
|
||||
version: 28.1.3
|
||||
resolution: "expect@npm:28.1.3"
|
||||
|
||||
Reference in New Issue
Block a user