From 3d55dfbe74e3e7f4d2e44e17a97b8f22e64a6be4 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Wed, 29 Jan 2025 14:36:11 -0800 Subject: [PATCH] Fix misplacement of http typekits. --- .../typekit/kits/http-operation.ts | 68 +++++---- .../typekit/kits/http-part.ts | 4 +- .../experimental/typekit/kits/http-request.ts | 40 ++---- .../typekit/kits/http-response.ts | 40 +----- .../src/experimental/typekit/kits/index.ts | 2 + .../typekit/kits/model-property.ts | 50 +------ .../{ => experimental}/typekit/kits/model.ts | 2 +- packages/http/src/typekit/index.ts | 1 - .../http/src/typekit/kits/http-operation.ts | 132 ------------------ .../http/src/typekit/kits/http-request.ts | 100 ------------- .../http/src/typekit/kits/http-response.ts | 43 ------ packages/http/src/typekit/kits/index.ts | 6 - .../http/src/typekit/kits/model-property.ts | 74 ---------- .../typekit/http-response.test.ts | 4 +- 14 files changed, 65 insertions(+), 501 deletions(-) rename packages/http/src/{ => experimental}/typekit/kits/http-part.ts (90%) rename packages/http/src/{ => experimental}/typekit/kits/model.ts (87%) delete mode 100644 packages/http/src/typekit/index.ts delete mode 100644 packages/http/src/typekit/kits/http-operation.ts delete mode 100644 packages/http/src/typekit/kits/http-request.ts delete mode 100644 packages/http/src/typekit/kits/http-response.ts delete mode 100644 packages/http/src/typekit/kits/index.ts delete mode 100644 packages/http/src/typekit/kits/model-property.ts diff --git a/packages/http/src/experimental/typekit/kits/http-operation.ts b/packages/http/src/experimental/typekit/kits/http-operation.ts index 23e153ed719..790b2130615 100644 --- a/packages/http/src/experimental/typekit/kits/http-operation.ts +++ b/packages/http/src/experimental/typekit/kits/http-operation.ts @@ -1,16 +1,32 @@ import { ignoreDiagnostics, Operation, StringLiteral, Type, VoidType } from "@typespec/compiler"; -import { defineKit, Typekit } from "@typespec/compiler/experimental/typekit"; +import { defineKit, Typekit } from "@typespec/compiler/typekit"; +import { HttpOperation, HttpOperationResponseContent, HttpStatusCodesEntry } from "../../../types.js"; import { getHttpOperation } from "../../../operations.js"; -import { - HttpOperation, - HttpOperationResponseContent, - HttpStatusCodesEntry, -} from "../../../types.js"; /** - * Utilities for working with HTTP operations. + * Structure of a flat HTTP response, which is grouped by status code and content type. * @experimental */ +export interface FlatHttpResponse { + /** + * Response status code. + */ + statusCode: HttpStatusCodesEntry; + /** + * Content type. Might be undefined if the response does not have a body. + */ + contentType?: string; + /** + * Response content. + */ + responseContent: HttpOperationResponseContent; + /** + * Response type. + * + */ + type: Type; +} + export interface HttpOperationKit { /** * Get the corresponding HTTP operation for the given TypeSpec operation. The same @@ -31,33 +47,11 @@ export interface HttpOperationKit { getReturnType(op: Operation, options?: { includeErrors?: boolean }): Type; } -/** - * Structure of a flat HTTP response, which is grouped by status code and content type. - */ -export interface FlatHttpResponse { - /** - * Response status code. - */ - statusCode: HttpStatusCodesEntry; - /** - * Content type. Might be undefined if the response does not have a body. - */ - contentType?: string; - /** - * Response content. - */ - responseContent: HttpOperationResponseContent; -} - interface TypekitExtension { - /** - * Utilities for working with HTTP operations. - * @experimental - */ httpOperation: HttpOperationKit; } -declare module "@typespec/compiler/experimental/typekit" { +declare module "@typespec/compiler/typekit" { interface Typekit extends TypekitExtension {} } @@ -66,11 +60,11 @@ defineKit({ get(op) { return ignoreDiagnostics(getHttpOperation(this.program, op)); }, - getReturnType(operation, options) { - let responses = this.httpOperation.getResponses(operation); + getReturnType(httpOperation, options) { + let responses = this.httpOperation.getResponses(httpOperation); if (!options?.includeErrors) { - responses = responses.filter((r) => !this.httpResponse.isErrorResponse(r.responseContent)); + responses = responses.filter((r) => !this.httpResponse.isErrorResponse(r)); } const voidType = { kind: "Intrinsic", name: "void" } as VoidType; @@ -112,7 +106,12 @@ defineKit({ contentType = "application/json"; } - responsesMap.push({ statusCode: response.statusCodes, contentType, responseContent }); + responsesMap.push({ + statusCode: response.statusCodes, + contentType, + responseContent, + type: response.type, + }); } } @@ -120,7 +119,6 @@ defineKit({ }, }, }); - function getEffectiveType(typekit: Typekit, type?: Type): Type { if (type === undefined) { return { kind: "Intrinsic", name: "void" } as VoidType; diff --git a/packages/http/src/typekit/kits/http-part.ts b/packages/http/src/experimental/typekit/kits/http-part.ts similarity index 90% rename from packages/http/src/typekit/kits/http-part.ts rename to packages/http/src/experimental/typekit/kits/http-part.ts index 1ffbca88640..83610e5e82a 100644 --- a/packages/http/src/typekit/kits/http-part.ts +++ b/packages/http/src/experimental/typekit/kits/http-part.ts @@ -1,6 +1,6 @@ import { Type } from "@typespec/compiler"; import { defineKit } from "@typespec/compiler/typekit"; -import { getHttpPart, HttpPart } from "../../private.decorators.js"; +import { getHttpPart, HttpPart } from "../../../private.decorators.js"; export interface HttpPartKit { /** @@ -20,7 +20,7 @@ export interface HttpPartKit { unpack(type: Type): Type; } -export interface TypekitExtension { +interface TypekitExtension { httpPart: HttpPartKit; } diff --git a/packages/http/src/experimental/typekit/kits/http-request.ts b/packages/http/src/experimental/typekit/kits/http-request.ts index df398586da8..dfb3e106613 100644 --- a/packages/http/src/experimental/typekit/kits/http-request.ts +++ b/packages/http/src/experimental/typekit/kits/http-request.ts @@ -1,5 +1,5 @@ import { Model, ModelProperty } from "@typespec/compiler"; -import { defineKit } from "@typespec/compiler/experimental/typekit"; +import { defineKit } from "@typespec/compiler/typekit"; import { HttpOperation } from "../../../types.js"; export type HttpRequestParameterKind = "query" | "header" | "path" | "contentType" | "body"; @@ -7,7 +7,7 @@ export type HttpRequestParameterKind = "query" | "header" | "path" | "contentTyp interface HttpRequestKit { body: { /** - * Checks the body is a property explicitly tagged with @body or @bodyRoot + * Checks the body is a property explicitly tagged with @body @bodyRoot or @multipartBody * @param httpOperation the http operation to check */ isExplicit(httpOperation: HttpOperation): boolean; @@ -32,7 +32,7 @@ interface TypekitExtension { httpRequest: HttpRequestKit; } -declare module "@typespec/compiler/experimental/typekit" { +declare module "@typespec/compiler/typekit" { interface Typekit extends TypekitExtension {} } @@ -42,7 +42,7 @@ defineKit({ isExplicit(httpOperation: HttpOperation) { return ( httpOperation.parameters.properties.find( - (p) => p.kind === "body" || p.kind === "bodyRoot", + (p) => p.kind === "body" || p.kind === "bodyRoot" || p.kind === "multipartBody", ) !== undefined ); }, @@ -74,35 +74,25 @@ defineKit({ kind: HttpRequestParameterKind | HttpRequestParameterKind[], ): Model | undefined { const kinds = new Set(Array.isArray(kind) ? kind : [kind]); - const parameterProperties: ModelProperty[] = []; + const parameterProperties = new Map(); - for (const kind of kinds) { + kinds.forEach((kind) => { if (kind === "body") { - const bodyParams = Array.from( - this.httpRequest.getBodyParameters(httpOperation)?.properties.values() ?? [], - ); - if (bodyParams) { - parameterProperties.push(...bodyParams); - } + this.httpRequest + .getBodyParameters(httpOperation) + ?.properties.forEach((value, key) => parameterProperties.set(key, value)); } else { - const params = httpOperation.parameters.properties - .filter((p) => p.kind === kind) - .map((p) => p.property); - parameterProperties.push(...params); + httpOperation.parameters.properties + .filter((p) => p.kind === kind && p.property) + .forEach((p) => parameterProperties.set(p.property!.name, p.property!)); } - } + }); - if (parameterProperties.length === 0) { + if (parameterProperties.size === 0) { return undefined; } - const properties = parameterProperties.reduce( - (acc, prop) => { - acc[prop.name] = prop; - return acc; - }, - {} as Record, - ); + const properties = Object.fromEntries(parameterProperties); return this.model.create({ properties }); }, diff --git a/packages/http/src/experimental/typekit/kits/http-response.ts b/packages/http/src/experimental/typekit/kits/http-response.ts index db91ae9b295..693f38fc052 100644 --- a/packages/http/src/experimental/typekit/kits/http-response.ts +++ b/packages/http/src/experimental/typekit/kits/http-response.ts @@ -1,58 +1,32 @@ import { isErrorModel } from "@typespec/compiler"; -import { defineKit } from "@typespec/compiler/experimental/typekit"; -import { - HttpOperationResponseContent, - HttpStatusCodeRange, - HttpStatusCodesEntry, -} from "../../../types.js"; +import { defineKit } from "@typespec/compiler/typekit"; +import { FlatHttpResponse } from "./http-operation.js"; +import { HttpStatusCodeRange, HttpStatusCodesEntry } from "../../../types.js"; -/** - * Utilities for working with HTTP responses. - * @experimental - */ -export interface HttpResponseKit { +interface HttpResponseKit { /** * Check if the response is an error response. */ - isErrorResponse(response: HttpOperationResponseContent): boolean; - /** - * utilities to perform checks on status codes - */ + isErrorResponse(response: FlatHttpResponse): boolean; statusCode: { - /** - * Check if the status code is a single status code - * @param statusCode status code to check - */ isSingle(statusCode: HttpStatusCodesEntry): statusCode is number; - /** - * Check if the status code is a range of status codes - * @param statusCode status code to check - */ isRange(statusCode: HttpStatusCodesEntry): statusCode is HttpStatusCodeRange; - /** - * Check if the status code is a default status code - * @param statusCode status code to check - */ isDefault(statusCode: HttpStatusCodesEntry): statusCode is "*"; }; } interface TypekitExtension { - /** - * Utilities for working with HTTP responses. - * @experimental - */ httpResponse: HttpResponseKit; } -declare module "@typespec/compiler/experimental/typekit" { +declare module "@typespec/compiler/typekit" { interface Typekit extends TypekitExtension {} } defineKit({ httpResponse: { isErrorResponse(response) { - return response.body ? isErrorModel(this.program, response.body.type) : false; + return this.model.is(response.type) ? isErrorModel(this.program, response.type) : false; }, statusCode: { isSingle(statusCode) { diff --git a/packages/http/src/experimental/typekit/kits/index.ts b/packages/http/src/experimental/typekit/kits/index.ts index 8f5054200dc..85c11796c4d 100644 --- a/packages/http/src/experimental/typekit/kits/index.ts +++ b/packages/http/src/experimental/typekit/kits/index.ts @@ -1,4 +1,6 @@ export * from "./http-operation.js"; +export * from "./http-part.js"; export * from "./http-request.js"; export * from "./http-response.js"; export * from "./model-property.js"; +export * from "./model.js"; diff --git a/packages/http/src/experimental/typekit/kits/model-property.ts b/packages/http/src/experimental/typekit/kits/model-property.ts index d6f706f4b64..0a7170d2d3f 100644 --- a/packages/http/src/experimental/typekit/kits/model-property.ts +++ b/packages/http/src/experimental/typekit/kits/model-property.ts @@ -1,62 +1,18 @@ import { ModelProperty } from "@typespec/compiler"; -import { defineKit } from "@typespec/compiler/experimental/typekit"; -import { - getHeaderFieldOptions, - getPathParamOptions, - getQueryParamOptions, - isHeader, - isMultipartBodyProperty, - isPathParam, - isQueryParam, -} from "../../../decorators.js"; +import { defineKit } from "@typespec/compiler/typekit"; import { HeaderFieldOptions, PathParameterOptions, QueryParameterOptions } from "../../../types.js"; +import { isHeader, getHeaderFieldOptions, isPathParam, getPathParamOptions, isQueryParam, getQueryParamOptions, isMultipartBodyProperty } from "../../../decorators.js"; -/** - * Utilities for working with model properties in the context of Http. - * @experimental - */ export interface HttpModelProperty { - /** - * Get the Http parameter options for a model property. - * @param prop a TypeSpec ModelProperty - */ getHttpParamOptions( prop: ModelProperty, ): HeaderFieldOptions | PathParameterOptions | QueryParameterOptions | undefined; - /** - * Get the Http header options for a model property. - * @param prop a TypeSpec ModelProperty - */ getHttpHeaderOptions(prop: ModelProperty): HeaderFieldOptions | undefined; - /** - * Get the Http path options for a model property. - * @param prop a TypeSpec ModelProperty - */ getHttpPathOptions(prop: ModelProperty): PathParameterOptions | undefined; - /** - * Get the Http query options for a model property. - * @param prop a TypeSpec ModelProperty - */ getHttpQueryOptions(prop: ModelProperty): QueryParameterOptions | undefined; - /** - * Check if a model property is an Http header. - * @param prop a TypeSpec ModelProperty - */ isHttpHeader(prop: ModelProperty): boolean; - /** - * Check if a model property is an Http path parameter. - * @param prop a TypeSpec ModelProperty - */ isHttpPathParam(prop: ModelProperty): boolean; - /** - * Check if a model property is an Http query parameter. - * @param prop a TypeSpec ModelProperty - */ isHttpQueryParam(prop: ModelProperty): boolean; - /** - * Check if a model property is an Http multipart body. - * @param prop a TypeSpec ModelProperty - */ isHttpMultipartBody(prop: ModelProperty): boolean; } @@ -64,7 +20,7 @@ interface TypekitExtension { modelProperty: HttpModelProperty; } -declare module "@typespec/compiler/experimental/typekit" { +declare module "@typespec/compiler/typekit" { interface ModelPropertyKit extends HttpModelProperty {} } diff --git a/packages/http/src/typekit/kits/model.ts b/packages/http/src/experimental/typekit/kits/model.ts similarity index 87% rename from packages/http/src/typekit/kits/model.ts rename to packages/http/src/experimental/typekit/kits/model.ts index 7bed43f727d..6e27b719b4c 100644 --- a/packages/http/src/typekit/kits/model.ts +++ b/packages/http/src/experimental/typekit/kits/model.ts @@ -1,6 +1,6 @@ import { Model } from "@typespec/compiler"; import { defineKit } from "@typespec/compiler/typekit"; -import { isHttpFile } from "../../private.decorators.js"; +import { isHttpFile } from "../../../private.decorators.js"; export interface HttpModel { isHttpFile(model: Model): boolean; diff --git a/packages/http/src/typekit/index.ts b/packages/http/src/typekit/index.ts deleted file mode 100644 index e69ceea9f2c..00000000000 --- a/packages/http/src/typekit/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./kits/index.js"; diff --git a/packages/http/src/typekit/kits/http-operation.ts b/packages/http/src/typekit/kits/http-operation.ts deleted file mode 100644 index a7a9c8615ef..00000000000 --- a/packages/http/src/typekit/kits/http-operation.ts +++ /dev/null @@ -1,132 +0,0 @@ -import { ignoreDiagnostics, Operation, StringLiteral, Type, VoidType } from "@typespec/compiler"; -import { defineKit, Typekit } from "@typespec/compiler/typekit"; -import { getHttpOperation } from "../../operations.js"; -import { HttpOperation, HttpOperationResponseContent, HttpStatusCodesEntry } from "../../types.js"; - -/** - * Structure of a flat HTTP response, which is grouped by status code and content type. - */ -export interface FlatHttpResponse { - /** - * Response status code. - */ - statusCode: HttpStatusCodesEntry; - /** - * Content type. Might be undefined if the response does not have a body. - */ - contentType?: string; - /** - * Response content. - */ - responseContent: HttpOperationResponseContent; - /** - * Response type. - * - */ - type: Type; -} - -export interface HttpOperationKit { - /** - * Get the corresponding HTTP operation for the given TypeSpec operation. The same - * TypeSpec operation will always return the exact same HttpOperation object. - * - * @param op The TypeSpec operation to get the HTTP operation metadata for. - */ - get(op: Operation): HttpOperation; - /** - * Get the responses for the given operation. This function will return an array of responses grouped by status code and content type. - * @param op operation to extract the HttpResponse from - */ - getResponses(op: Operation): FlatHttpResponse[]; - /** - * Get the Http Return type for the given operation. This function will resolve the returnType based on the Http Operation. - * @param op operation to get the return type for - */ - getReturnType(op: Operation, options?: { includeErrors?: boolean }): Type; -} - -interface TypekitExtension { - httpOperation: HttpOperationKit; -} - -declare module "@typespec/compiler/typekit" { - interface Typekit extends TypekitExtension {} -} - -defineKit({ - httpOperation: { - get(op) { - return ignoreDiagnostics(getHttpOperation(this.program, op)); - }, - getReturnType(operation, options) { - const httpOperation = this.httpOperation.get(operation); - let responses = this.httpOperation.getResponses(httpOperation.operation); - - if (!options?.includeErrors) { - responses = responses.filter((r) => !this.httpResponse.isErrorResponse(r)); - } - - const voidType = { kind: "Intrinsic", name: "void" } as VoidType; - let httpReturnType: Type = voidType; - - if (!responses.length) { - return voidType; - } - - if (responses.length > 1) { - const res = [...new Set(responses.map((r) => r.responseContent.body?.type))]; - httpReturnType = this.union.create({ - variants: res.map((t) => { - return this.unionVariant.create({ - type: getEffectiveType(this, t), - }); - }), - }); - } else { - httpReturnType = getEffectiveType(this, responses[0].responseContent.body?.type); - } - - return httpReturnType; - }, - getResponses(operation) { - const responsesMap: FlatHttpResponse[] = []; - const httpOperation = this.httpOperation.get(operation); - for (const response of httpOperation.responses) { - for (const responseContent of response.responses) { - const contentTypeProperty = responseContent.properties.find( - (property) => property.kind === "contentType", - ); - - let contentType: string | undefined; - - if (contentTypeProperty) { - contentType = (contentTypeProperty.property.type as StringLiteral).value; - } else if (responseContent.body) { - contentType = "application/json"; - } - - responsesMap.push({ - statusCode: response.statusCodes, - contentType, - responseContent, - type: response.type, - }); - } - } - - return responsesMap; - }, - }, -}); - -function getEffectiveType(typekit: Typekit, type?: Type): Type { - if (type === undefined) { - return { kind: "Intrinsic", name: "void" } as VoidType; - } - if (typekit.model.is(type)) { - return typekit.model.getEffectiveModel(type); - } - - return type; -} diff --git a/packages/http/src/typekit/kits/http-request.ts b/packages/http/src/typekit/kits/http-request.ts deleted file mode 100644 index 5963a7d1e9c..00000000000 --- a/packages/http/src/typekit/kits/http-request.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { Model, ModelProperty } from "@typespec/compiler"; -import { defineKit } from "@typespec/compiler/typekit"; -import { HttpOperation } from "../../types.js"; - -export type HttpRequestParameterKind = "query" | "header" | "path" | "contentType" | "body"; - -interface HttpRequestKit { - body: { - /** - * Checks the body is a property explicitly tagged with @body @bodyRoot or @multipartBody - * @param httpOperation the http operation to check - */ - isExplicit(httpOperation: HttpOperation): boolean; - }; - /** - * Gets a Model representing the body parameters of an http operation. - * @param httpOperation the http operation to get the body parameters from - */ - getBodyParameters(httpOperation: HttpOperation): Model | undefined; - /** - * Gets a Model representing the parameters of an http operation. - * @param httpOperation The Http operation to get the parameters from. - * @param kind A string to filters specific parameter kinds, or an array to combine multiple kinds. - */ - getParameters( - httpOperation: HttpOperation, - kind: HttpRequestParameterKind[] | HttpRequestParameterKind, - ): Model | undefined; -} - -interface TypekitExtension { - httpRequest: HttpRequestKit; -} - -declare module "@typespec/compiler/typekit" { - interface Typekit extends TypekitExtension {} -} - -defineKit({ - httpRequest: { - body: { - isExplicit(httpOperation: HttpOperation) { - return ( - httpOperation.parameters.properties.find( - (p) => p.kind === "body" || p.kind === "bodyRoot" || p.kind === "multipartBody", - ) !== undefined - ); - }, - }, - getBodyParameters(httpOperation: HttpOperation): Model | undefined { - const body = httpOperation.parameters.body; - - if (!body) { - return undefined; - } - - const bodyProperty = body.property; - - if (!bodyProperty) { - if (body.type.kind === "Model") { - return body.type; - } - throw new Error("Body property not found"); - } - - const bodyPropertyName = bodyProperty.name ? bodyProperty.name : "body"; - - return this.model.create({ - properties: { [bodyPropertyName]: bodyProperty }, - }); - }, - getParameters( - httpOperation: HttpOperation, - kind: HttpRequestParameterKind | HttpRequestParameterKind[], - ): Model | undefined { - const kinds = new Set(Array.isArray(kind) ? kind : [kind]); - const parameterProperties = new Map(); - - kinds.forEach((kind) => { - if (kind === "body") { - this.httpRequest - .getBodyParameters(httpOperation) - ?.properties.forEach((value, key) => parameterProperties.set(key, value)); - } else { - httpOperation.parameters.properties - .filter((p) => p.kind === kind && p.property) - .forEach((p) => parameterProperties.set(p.property!.name, p.property!)); - } - }); - - if (parameterProperties.size === 0) { - return undefined; - } - - const properties = Object.fromEntries(parameterProperties); - - return this.model.create({ properties }); - }, - }, -}); diff --git a/packages/http/src/typekit/kits/http-response.ts b/packages/http/src/typekit/kits/http-response.ts deleted file mode 100644 index df659a2fe78..00000000000 --- a/packages/http/src/typekit/kits/http-response.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { isErrorModel } from "@typespec/compiler"; -import { defineKit } from "@typespec/compiler/typekit"; -import { HttpStatusCodeRange, HttpStatusCodesEntry } from "../../types.js"; -import { FlatHttpResponse } from "./http-operation.js"; - -interface HttpResponseKit { - /** - * Check if the response is an error response. - */ - isErrorResponse(response: FlatHttpResponse): boolean; - statusCode: { - isSingle(statusCode: HttpStatusCodesEntry): statusCode is number; - isRange(statusCode: HttpStatusCodesEntry): statusCode is HttpStatusCodeRange; - isDefault(statusCode: HttpStatusCodesEntry): statusCode is "*"; - }; -} - -interface TypekitExtension { - httpResponse: HttpResponseKit; -} - -declare module "@typespec/compiler/typekit" { - interface Typekit extends TypekitExtension {} -} - -defineKit({ - httpResponse: { - isErrorResponse(response) { - return this.model.is(response.type) ? isErrorModel(this.program, response.type) : false; - }, - statusCode: { - isSingle(statusCode) { - return typeof statusCode === "number"; - }, - isRange(statusCode) { - return typeof statusCode === "object" && "start" in statusCode && "end" in statusCode; - }, - isDefault(statusCode) { - return statusCode === "*"; - }, - }, - }, -}); diff --git a/packages/http/src/typekit/kits/index.ts b/packages/http/src/typekit/kits/index.ts deleted file mode 100644 index 85c11796c4d..00000000000 --- a/packages/http/src/typekit/kits/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * from "./http-operation.js"; -export * from "./http-part.js"; -export * from "./http-request.js"; -export * from "./http-response.js"; -export * from "./model-property.js"; -export * from "./model.js"; diff --git a/packages/http/src/typekit/kits/model-property.ts b/packages/http/src/typekit/kits/model-property.ts deleted file mode 100644 index dbc17b81020..00000000000 --- a/packages/http/src/typekit/kits/model-property.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { ModelProperty } from "@typespec/compiler"; -import { defineKit } from "@typespec/compiler/typekit"; -import { - getHeaderFieldOptions, - getPathParamOptions, - getQueryParamOptions, - isHeader, - isMultipartBodyProperty, - isPathParam, - isQueryParam, -} from "../../decorators.js"; -import { HeaderFieldOptions, PathParameterOptions, QueryParameterOptions } from "../../types.js"; - -export interface HttpModelProperty { - getHttpParamOptions( - prop: ModelProperty, - ): HeaderFieldOptions | PathParameterOptions | QueryParameterOptions | undefined; - getHttpHeaderOptions(prop: ModelProperty): HeaderFieldOptions | undefined; - getHttpPathOptions(prop: ModelProperty): PathParameterOptions | undefined; - getHttpQueryOptions(prop: ModelProperty): QueryParameterOptions | undefined; - isHttpHeader(prop: ModelProperty): boolean; - isHttpPathParam(prop: ModelProperty): boolean; - isHttpQueryParam(prop: ModelProperty): boolean; - isHttpMultipartBody(prop: ModelProperty): boolean; -} - -interface TypekitExtension { - modelProperty: HttpModelProperty; -} - -declare module "@typespec/compiler/typekit" { - interface ModelPropertyKit extends HttpModelProperty {} -} - -defineKit({ - modelProperty: { - getHttpParamOptions(prop: ModelProperty) { - if (isHeader(this.program, prop)) { - return getHeaderFieldOptions(this.program, prop); - } - - if (isPathParam(this.program, prop)) { - return getPathParamOptions(this.program, prop); - } - - if (isQueryParam(this.program, prop)) { - return getQueryParamOptions(this.program, prop); - } - - return undefined; - }, - getHttpHeaderOptions(prop: ModelProperty) { - return getHeaderFieldOptions(this.program, prop); - }, - getHttpPathOptions(prop) { - return getPathParamOptions(this.program, prop); - }, - getHttpQueryOptions(prop: ModelProperty) { - return getQueryParamOptions(this.program, prop); - }, - isHttpHeader(prop: ModelProperty) { - return isHeader(this.program, prop); - }, - isHttpPathParam(prop: ModelProperty) { - return isPathParam(this.program, prop); - }, - isHttpQueryParam(prop: ModelProperty) { - return isQueryParam(this.program, prop); - }, - isHttpMultipartBody(prop: ModelProperty) { - return isMultipartBodyProperty(this.program, prop); - }, - }, -}); diff --git a/packages/http/test/experimental/typekit/http-response.test.ts b/packages/http/test/experimental/typekit/http-response.test.ts index 19590da612c..352789dbd58 100644 --- a/packages/http/test/experimental/typekit/http-response.test.ts +++ b/packages/http/test/experimental/typekit/http-response.test.ts @@ -34,8 +34,8 @@ it("should return true for an error response", async () => { const responses = $.httpOperation.getResponses(getFoo); expect(responses).toHaveLength(2); - expect($.httpResponse.isErrorResponse(responses[0].responseContent)).toBe(false); - expect($.httpResponse.isErrorResponse(responses[1].responseContent)).toBe(true); + expect($.httpResponse.isErrorResponse(responses[0])).toBe(false); + expect($.httpResponse.isErrorResponse(responses[1])).toBe(true); }); it("should identify a single and default status code", async () => {