diff --git a/.husky/pre-commit b/.husky/pre-commit index d2ae35e84b..8862d8db03 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/zsh . "$(dirname "$0")/_/husky.sh" yarn lint-staged diff --git a/plugins/openapi-router/package.json b/plugins/openapi-router/package.json index 695a7bfc4d..dfc8b85441 100644 --- a/plugins/openapi-router/package.json +++ b/plugins/openapi-router/package.json @@ -24,7 +24,8 @@ "postpack": "backstage-cli package postpack" }, "devDependencies": { - "@backstage/cli": "workspace:^" + "@backstage/cli": "workspace:^", + "expect-type": "^0.15.0" }, "files": [ "dist" diff --git a/plugins/openapi-router/src/types/immutable.ts b/plugins/openapi-router/src/types/immutable.ts index dec20105cf..7cba014e0a 100644 --- a/plugins/openapi-router/src/types/immutable.ts +++ b/plugins/openapi-router/src/types/immutable.ts @@ -18,6 +18,7 @@ import type { OpenAPIObject, ReferenceObject, RequestBodyObject, + ParameterObject, } from 'openapi3-ts'; /** @@ -68,3 +69,33 @@ export type ImmutableContentObject = ImmutableObject; * @public */ export type ImmutableRequestBodyObject = ImmutableObject; + +export type ImmutableParameterObject = ImmutableObject; + +export interface HeaderObject extends ParameterObject { + in: 'header'; + style: 'simple'; +} + +export type ImmutableHeaderObject = ImmutableObject; + +export interface CookieObject extends ParameterObject { + in: 'cookie'; + style: 'form'; +} + +export type ImmutableCookieObject = ImmutableObject; + +export interface QueryObject extends ParameterObject { + in: 'query'; + style: 'form' | 'deepObject' | 'pipeDelimited' | 'spaceDelimited'; +} + +export type ImmutableQueryObject = ImmutableObject; + +export interface PathObject extends ParameterObject { + in: 'path'; + style: 'simple' | 'label' | 'matrix'; +} + +export type ImmutablePathObject = ImmutableObject; diff --git a/plugins/openapi-router/src/types/params.ts b/plugins/openapi-router/src/types/params.ts new file mode 100644 index 0000000000..3e06e266dd --- /dev/null +++ b/plugins/openapi-router/src/types/params.ts @@ -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, + 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 + ? ComponentRef< + Doc, + 'parameters', + DocOperation['parameter'][Parameter] + > + : never + : DocOperation['parameters'][Parameter]; + +export type DocParameter< + Doc extends RequiredDoc, + Path extends Extract, + Method extends keyof Doc['paths'][Path], + Parameter extends keyof Doc['paths'][Path][Method]['parameters'], +> = ResolveDocParameter< + Doc, + Path, + Method, + Parameter +> extends ImmutableParameterObject + ? ResolveDocParameter + : never; + +type KeysMatching = { + [K in keyof T]-?: T[K] extends V ? K : never; +}[keyof T]; +/** + * @public + */ +export type ConvertAll = []> = T extends [ + infer First extends JSONSchema7, + ...infer Rest, +] + ? ConvertAll]> + : R; +/** + * @public + */ +export type DocParameters< + Doc extends RequiredDoc, + Path extends DocPathTemplate, + Method extends keyof Doc['paths'][Path], +> = TuplifyUnion<>; + +export type HeaderSchema< + Doc extends RequiredDoc, + Path extends PathTemplate>, + Method extends keyof Doc['paths'][Path], +> = KeysMatching, ImmutableHeaderObject>; + +type keys = HeaderSchema; + +type match = KeysMatching<[1, 2, 3], 1>; diff --git a/yarn.lock b/yarn.lock index 7601b411e7..1442f4d64e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"