diff --git a/modules/openapi-generator/src/main/resources/typescript/http/http.mustache b/modules/openapi-generator/src/main/resources/typescript/http/http.mustache index b7ec2295711..0ecca57cd86 100644 --- a/modules/openapi-generator/src/main/resources/typescript/http/http.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/http/http.mustache @@ -63,6 +63,20 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + {{#platforms}} + {{#node}} + throw new Error("You need to define an absolute base url for the server."); + {{/node}} + {{^node}} + return window.location.origin + url; + {{/node}} + {{/platforms}} +} + /** * Represents an HTTP request context */ @@ -83,7 +97,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -101,7 +115,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/client/others/typescript/builds/with-unique-items/http/http.ts b/samples/client/others/typescript/builds/with-unique-items/http/http.ts index 5ec4785ede3..e8da8b355da 100644 --- a/samples/client/others/typescript/builds/with-unique-items/http/http.ts +++ b/samples/client/others/typescript/builds/with-unique-items/http/http.ts @@ -33,6 +33,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + return window.location.origin + url; +} + /** * Represents an HTTP request context */ @@ -48,7 +55,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -66,7 +73,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/builds/browser/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/browser/http/http.ts index 5ec4785ede3..e8da8b355da 100644 --- a/samples/openapi3/client/petstore/typescript/builds/browser/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/browser/http/http.ts @@ -33,6 +33,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + return window.location.origin + url; +} + /** * Represents an HTTP request context */ @@ -48,7 +55,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -66,7 +73,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/builds/composed-schemas/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/composed-schemas/http/http.ts index 5ec4785ede3..e8da8b355da 100644 --- a/samples/openapi3/client/petstore/typescript/builds/composed-schemas/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/composed-schemas/http/http.ts @@ -33,6 +33,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + return window.location.origin + url; +} + /** * Represents an HTTP request context */ @@ -48,7 +55,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -66,7 +73,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts index 50cca24fe84..af0075aeba6 100644 --- a/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts @@ -41,6 +41,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + throw new Error("You need to define an absolute base url for the server."); +} + /** * Represents an HTTP request context */ @@ -57,7 +64,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -75,7 +82,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/builds/deno/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/deno/http/http.ts index a286eac8a41..97a04c3e207 100644 --- a/samples/openapi3/client/petstore/typescript/builds/deno/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/deno/http/http.ts @@ -32,6 +32,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + return window.location.origin + url; +} + /** * Represents an HTTP request context */ @@ -47,7 +54,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -65,7 +72,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts index 50cca24fe84..af0075aeba6 100644 --- a/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts @@ -41,6 +41,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + throw new Error("You need to define an absolute base url for the server."); +} + /** * Represents an HTTP request context */ @@ -57,7 +64,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -75,7 +82,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/builds/jquery/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/jquery/http/http.ts index 4593d0f450c..81f327aa1ee 100644 --- a/samples/openapi3/client/petstore/typescript/builds/jquery/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/jquery/http/http.ts @@ -33,6 +33,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + return window.location.origin + url; +} + /** * Represents an HTTP request context */ @@ -48,7 +55,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -66,7 +73,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts index 50cca24fe84..af0075aeba6 100644 --- a/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts @@ -41,6 +41,13 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +function ensureAbsoluteUrl(url: string) { + if (url.startsWith("http://") || url.startsWith("https://")) { + return url; + } + throw new Error("You need to define an absolute base url for the server."); +} + /** * Represents an HTTP request context */ @@ -57,7 +64,7 @@ export class RequestContext { * @param httpMethod http method */ public constructor(url: string, private httpMethod: HttpMethod) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /* @@ -75,7 +82,7 @@ export class RequestContext { * */ public setUrl(url: string) { - this.url = new URL(url); + this.url = new URL(ensureAbsoluteUrl(url)); } /** diff --git a/samples/openapi3/client/petstore/typescript/tests/browser/test/ServerConfiguration.test.ts b/samples/openapi3/client/petstore/typescript/tests/browser/test/ServerConfiguration.test.ts new file mode 100644 index 00000000000..28b817fe296 --- /dev/null +++ b/samples/openapi3/client/petstore/typescript/tests/browser/test/ServerConfiguration.test.ts @@ -0,0 +1,19 @@ +import { expect } from '@esm-bundle/chai'; +import { ServerConfiguration, HttpMethod } from 'ts-petstore-client' + +describe("ServerConfiguration", () => { + it("supports absolute http URLs", async () => { + const config = new ServerConfiguration("http://localhost/v2", {}); + expect(config.makeRequestContext("/resource", HttpMethod.PUT).getUrl()).to.equal("http://localhost/v2/resource"); + }) + + it("supports absolute https URLs", async () => { + const config = new ServerConfiguration("https://localhost/v2", {}); + expect(config.makeRequestContext("/resource", HttpMethod.PUT).getUrl()).to.equal("https://localhost/v2/resource"); + }) + + it("supports relative URLs", async () => { + const config = new ServerConfiguration("/api", {}); + expect(config.makeRequestContext("/resource", HttpMethod.PUT).getUrl()).to.equal("http://localhost:8080/api/resource"); + }) +}) diff --git a/samples/openapi3/client/petstore/typescript/tests/browser/web-test-runner.config.js b/samples/openapi3/client/petstore/typescript/tests/browser/web-test-runner.config.js index db096833265..0b3466fb9d9 100644 --- a/samples/openapi3/client/petstore/typescript/tests/browser/web-test-runner.config.js +++ b/samples/openapi3/client/petstore/typescript/tests/browser/web-test-runner.config.js @@ -4,6 +4,7 @@ export default { files: "./dist/*.test.js", nodeResolve: true, manual: false, + port: 8080, browsers: [ puppeteerLauncher(), ], diff --git a/samples/openapi3/client/petstore/typescript/tests/inversify/test/services.test.ts b/samples/openapi3/client/petstore/typescript/tests/inversify/test/services.test.ts index 264dbad31af..f1bb264554b 100644 --- a/samples/openapi3/client/petstore/typescript/tests/inversify/test/services.test.ts +++ b/samples/openapi3/client/petstore/typescript/tests/inversify/test/services.test.ts @@ -2,7 +2,7 @@ import "reflect-metadata"; import { Container } from "inversify"; import * as petstore from "ts-petstore-client"; -import * as petstoreInternals from "ts-petstore-client/dist/apis/PetApi"; +import * as petstoreInternals from "../../../builds/inversify/dist/apis/PetApi"; import { expect, assert } from "chai"; import * as fs from "fs"; @@ -20,7 +20,7 @@ describe("ApiServiceBinder", () => { }); it("binds server config", async () => { - const url = "foobar"; + const url = "http://foobar"; let callCount = 0; const mockServer = { makeRequestContext(endpoint: string, httpMethod: petstore.HttpMethod): petstore.RequestContext { @@ -45,7 +45,7 @@ describe("ApiServiceBinder", () => { }); it("binds server config to url", async () => { - const url = "foobar"; + const url = "http://foobar"; const petId = 42; apiServiceBinder.bindServerConfigurationToURL(url);