Skip to content

Commit

Permalink
Allowing endpoints that don't end in '/' (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
glecaros authored Jun 10, 2024
1 parent 22b343e commit fd9789b
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions sdk/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,16 @@ function handleFailedRequest(status: string, body: unknown): never {
};
}

function splitURL(url: string): [string, string] {
const parsed = new URL(url);
return [parsed.origin, parsed.pathname];
}

export class AIChatProtocolClient {
private client: Client;

private basePath: string;

constructor(endpoint: string, options?: AIChatClientOptions);
constructor(
endpoint: string,
Expand All @@ -82,13 +89,15 @@ export class AIChatProtocolClient {
arg2?: AIChatClientOptions,
) {
const absoluteEndpoint = toAbsoluteUrl(endpoint);
const [origin, basePath] = splitURL(absoluteEndpoint);
this.basePath = basePath;
const defaults: AIChatClientOptions = {
allowInsecureConnection: isLocalhost(absoluteEndpoint),
};
if (isCredential(arg1)) {
this.client = getClient(absoluteEndpoint, arg1, { ...defaults, ...arg2 });
this.client = getClient(origin, arg1, { ...defaults, ...arg2 });
} else {
this.client = getClient(absoluteEndpoint, { ...defaults, ...arg1 });
this.client = getClient(origin, { ...defaults, ...arg1 });
}
}

Expand All @@ -113,7 +122,9 @@ export class AIChatProtocolClient {
sessionState: options.sessionState,
},
};
const response = await this.client.path("/").post(request, options);
const response = await this.client
.path(this.basePath)
.post(request, options);
const { status, body } = response;
if (!/2\d\d/.test(status)) {
handleFailedRequest(status, body);
Expand Down Expand Up @@ -143,7 +154,7 @@ export class AIChatProtocolClient {
},
};
const response = await asStream(
this.client.path("/stream").post(request, options),
this.client.path(`${this.basePath}/stream`).post(request, options),
);
if (!/2\d\d/.test(response.status)) {
const body = await getStreamContent(response.body);
Expand Down

0 comments on commit fd9789b

Please sign in to comment.