Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-baboons-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

Fix how headers are handled in @hey-api/client-angular#3860
24 changes: 19 additions & 5 deletions examples/openapi-ts-angular-common/src/client/client/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { HttpResponse } from '@angular/common/http';
import type { HttpHeaders, HttpResponse } from '@angular/common/http';
import { HttpClient, HttpErrorResponse, HttpEventType, HttpRequest } from '@angular/common/http';
import {
assertInInjectionContext,
Expand Down Expand Up @@ -87,14 +87,26 @@ export const createClient = (config: Config = {}): Client => {
opts.headers = opts.headers.delete('Content-Type');
}

return opts;
};

const finalizeRequest = <
TData = unknown,
ThrowOnError extends boolean = false,
TResponseStyle extends ResponseStyle = 'fields',
>(
opts: RequestOptions<TData, TResponseStyle, ThrowOnError> & {
headers: HttpHeaders;
},
) => {
const url = buildUrl(opts as Config & RequestOptions);

const req = new HttpRequest<unknown>(opts.method ?? 'GET', url, getValidRequestBody(opts), {
redirect: 'follow',
...opts,
});

return { opts, req, url };
return { req, url };
};

const beforeRequest = async <
Expand All @@ -105,16 +117,18 @@ export const createClient = (config: Config = {}): Client => {
>(
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
) => {
const { opts, req, url } = requestOptions(options);
const opts = requestOptions(options);

if (opts.security) {
await setAuthParams(opts);
await setAuthParams(opts, opts.security);
}

if (opts.requestValidator) {
await opts.requestValidator(opts);
}

const { req, url } = finalizeRequest(opts);

return { opts, req, url };
};

Expand Down Expand Up @@ -236,7 +250,7 @@ export const createClient = (config: Config = {}): Client => {
throw new Error('Request validation is not supported in requestOptions');
}

return requestOptions(options).req;
return finalizeRequest(requestOptions(options)).req;
},
setConfig,
sse: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ export const getParseAs = (
return;
};

export async function setAuthParams(
options: Pick<RequestOptions, 'auth' | 'query' | 'security'> & {
export const setAuthParams = async (
options: Pick<RequestOptions, 'auth' | 'query'> & {
headers: HttpHeaders;
},
): Promise<void> {
for (const auth of options.security ?? []) {
security: Pick<Required<RequestOptions>, 'security'>['security'],
) => {
for (const auth of security) {
const token = await getAuthToken(auth, options.auth);

if (!token) {
Expand All @@ -208,10 +209,8 @@ export async function setAuthParams(
options.headers = options.headers.set(name, token);
break;
}

return;
}
}
};

export const buildUrl: Client['buildUrl'] = (options) => {
const url = getUrl({
Expand Down
24 changes: 19 additions & 5 deletions examples/openapi-ts-angular/src/client/client/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { HttpResponse } from '@angular/common/http';
import type { HttpHeaders, HttpResponse } from '@angular/common/http';
import { HttpClient, HttpErrorResponse, HttpEventType, HttpRequest } from '@angular/common/http';
import {
assertInInjectionContext,
Expand Down Expand Up @@ -87,14 +87,26 @@ export const createClient = (config: Config = {}): Client => {
opts.headers = opts.headers.delete('Content-Type');
}

return opts;
};

const finalizeRequest = <
TData = unknown,
ThrowOnError extends boolean = false,
TResponseStyle extends ResponseStyle = 'fields',
>(
opts: RequestOptions<TData, TResponseStyle, ThrowOnError> & {
headers: HttpHeaders;
},
) => {
const url = buildUrl(opts as Config & RequestOptions);

const req = new HttpRequest<unknown>(opts.method ?? 'GET', url, getValidRequestBody(opts), {
redirect: 'follow',
...opts,
});

return { opts, req, url };
return { req, url };
};

const beforeRequest = async <
Expand All @@ -105,16 +117,18 @@ export const createClient = (config: Config = {}): Client => {
>(
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
) => {
const { opts, req, url } = requestOptions(options);
const opts = requestOptions(options);

if (opts.security) {
await setAuthParams(opts);
await setAuthParams(opts, opts.security);
}

if (opts.requestValidator) {
await opts.requestValidator(opts);
}

const { req, url } = finalizeRequest(opts);

return { opts, req, url };
};

Expand Down Expand Up @@ -236,7 +250,7 @@ export const createClient = (config: Config = {}): Client => {
throw new Error('Request validation is not supported in requestOptions');
}

return requestOptions(options).req;
return finalizeRequest(requestOptions(options)).req;
},
setConfig,
sse: {
Expand Down
13 changes: 6 additions & 7 deletions examples/openapi-ts-angular/src/client/client/utils.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ export const getParseAs = (
return;
};

export async function setAuthParams(
options: Pick<RequestOptions, 'auth' | 'query' | 'security'> & {
export const setAuthParams = async (
options: Pick<RequestOptions, 'auth' | 'query'> & {
headers: HttpHeaders;
},
): Promise<void> {
for (const auth of options.security ?? []) {
security: Pick<Required<RequestOptions>, 'security'>['security'],
) => {
for (const auth of security) {
const token = await getAuthToken(auth, options.auth);

if (!token) {
Expand All @@ -208,10 +209,8 @@ export async function setAuthParams(
options.headers = options.headers.set(name, token);
break;
}

return;
}
}
};

export const buildUrl: Client['buildUrl'] = (options) => {
const url = getUrl({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { HttpResponse } from '@angular/common/http';
import type { HttpHeaders, HttpResponse } from '@angular/common/http';
import { HttpClient, HttpErrorResponse, HttpEventType, HttpRequest } from '@angular/common/http';
import {
assertInInjectionContext,
Expand Down Expand Up @@ -87,14 +87,26 @@ export const createClient = (config: Config = {}): Client => {
opts.headers = opts.headers.delete('Content-Type');
}

return opts;
};

const finalizeRequest = <
TData = unknown,
ThrowOnError extends boolean = false,
TResponseStyle extends ResponseStyle = 'fields',
>(
opts: RequestOptions<TData, TResponseStyle, ThrowOnError> & {
headers: HttpHeaders;
},
) => {
const url = buildUrl(opts as Config & RequestOptions);

const req = new HttpRequest<unknown>(opts.method ?? 'GET', url, getValidRequestBody(opts), {
redirect: 'follow',
...opts,
});

return { opts, req, url };
return { req, url };
};

const beforeRequest = async <
Expand All @@ -105,16 +117,18 @@ export const createClient = (config: Config = {}): Client => {
>(
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
) => {
const { opts, req, url } = requestOptions(options);
const opts = requestOptions(options);

if (opts.security) {
await setAuthParams(opts);
await setAuthParams(opts, opts.security);
}

if (opts.requestValidator) {
await opts.requestValidator(opts);
}

const { req, url } = finalizeRequest(opts);

return { opts, req, url };
};

Expand Down Expand Up @@ -236,7 +250,7 @@ export const createClient = (config: Config = {}): Client => {
throw new Error('Request validation is not supported in requestOptions');
}

return requestOptions(options).req;
return finalizeRequest(requestOptions(options)).req;
},
setConfig,
sse: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ export const getParseAs = (
return;
};

export async function setAuthParams(
options: Pick<RequestOptions, 'auth' | 'query' | 'security'> & {
export const setAuthParams = async (
options: Pick<RequestOptions, 'auth' | 'query'> & {
headers: HttpHeaders;
},
): Promise<void> {
for (const auth of options.security ?? []) {
security: Pick<Required<RequestOptions>, 'security'>['security'],
) => {
for (const auth of security) {
const token = await getAuthToken(auth, options.auth);

if (!token) {
Expand All @@ -208,10 +209,8 @@ export async function setAuthParams(
options.headers = options.headers.set(name, token);
break;
}

return;
}
}
};

export const buildUrl: Client['buildUrl'] = (options) => {
const url = getUrl({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { HttpResponse } from '@angular/common/http';
import type { HttpHeaders, HttpResponse } from '@angular/common/http';
import { HttpClient, HttpErrorResponse, HttpEventType, HttpRequest } from '@angular/common/http';
import {
assertInInjectionContext,
Expand Down Expand Up @@ -87,14 +87,26 @@ export const createClient = (config: Config = {}): Client => {
opts.headers = opts.headers.delete('Content-Type');
}

return opts;
};

const finalizeRequest = <
TData = unknown,
ThrowOnError extends boolean = false,
TResponseStyle extends ResponseStyle = 'fields',
>(
opts: RequestOptions<TData, TResponseStyle, ThrowOnError> & {
headers: HttpHeaders;
},
) => {
const url = buildUrl(opts as Config & RequestOptions);

const req = new HttpRequest<unknown>(opts.method ?? 'GET', url, getValidRequestBody(opts), {
redirect: 'follow',
...opts,
});

return { opts, req, url };
return { req, url };
};

const beforeRequest = async <
Expand All @@ -105,16 +117,18 @@ export const createClient = (config: Config = {}): Client => {
>(
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
) => {
const { opts, req, url } = requestOptions(options);
const opts = requestOptions(options);

if (opts.security) {
await setAuthParams(opts);
await setAuthParams(opts, opts.security);
}

if (opts.requestValidator) {
await opts.requestValidator(opts);
}

const { req, url } = finalizeRequest(opts);

return { opts, req, url };
};

Expand Down Expand Up @@ -236,7 +250,7 @@ export const createClient = (config: Config = {}): Client => {
throw new Error('Request validation is not supported in requestOptions');
}

return requestOptions(options).req;
return finalizeRequest(requestOptions(options)).req;
},
setConfig,
sse: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ export const getParseAs = (
return;
};

export async function setAuthParams(
options: Pick<RequestOptions, 'auth' | 'query' | 'security'> & {
export const setAuthParams = async (
options: Pick<RequestOptions, 'auth' | 'query'> & {
headers: HttpHeaders;
},
): Promise<void> {
for (const auth of options.security ?? []) {
security: Pick<Required<RequestOptions>, 'security'>['security'],
) => {
for (const auth of security) {
const token = await getAuthToken(auth, options.auth);

if (!token) {
Expand All @@ -211,10 +212,8 @@ export async function setAuthParams(
options.headers = options.headers.set(name, token);
break;
}

return;
}
}
};

export const buildUrl: Client['buildUrl'] = (options) => {
const url = getUrl({
Expand Down
Loading
Loading