fork of hey-api/openapi-ts because I need some additional things

fix: update setAuthParams to use HttpHeaders and improve header handling

Max Scopp 000eebec 3d933d90

+42 -38
+33 -28
packages/openapi-ts/src/plugins/@hey-api/client-angular/__tests__/utils.test.ts
··· 1 + import { HttpHeaders } from '@angular/common/http'; 1 2 import { describe, expect, it, vi } from 'vitest'; 2 3 3 4 import type { Auth } from '../../client-core/bundle/auth'; ··· 91 92 describe('setAuthParams', () => { 92 93 it('sets bearer token in headers', async () => { 93 94 const auth = vi.fn().mockReturnValue('foo'); 94 - const headers = new Headers(); 95 + const headers = new HttpHeaders(); 95 96 const query: Record<any, unknown> = {}; 96 - await setAuthParams({ 97 + const options = { 97 98 auth, 98 99 headers, 99 100 query, 100 101 security: [ 101 102 { 102 103 name: 'baz', 103 - scheme: 'bearer', 104 - type: 'http', 104 + scheme: 'bearer' as const, 105 + type: 'http' as const, 105 106 }, 106 107 ], 107 - }); 108 + }; 109 + await setAuthParams(options); 108 110 expect(auth).toHaveBeenCalled(); 109 - expect(headers.get('baz')).toBe('Bearer foo'); 111 + expect(options.headers.get('baz')).toBe('Bearer foo'); 110 112 expect(Object.keys(query).length).toBe(0); 111 113 }); 112 114 113 115 it('sets access token in query', async () => { 114 116 const auth = vi.fn().mockReturnValue('foo'); 115 - const headers = new Headers(); 117 + const headers = new HttpHeaders(); 116 118 const query: Record<any, unknown> = {}; 117 119 await setAuthParams({ 118 120 auth, ··· 134 136 135 137 it('sets Authorization header when `in` and `name` are undefined', async () => { 136 138 const auth = vi.fn().mockReturnValue('foo'); 137 - const headers = new Headers(); 139 + const headers = new HttpHeaders(); 138 140 const query: Record<any, unknown> = {}; 139 - await setAuthParams({ 141 + const options = { 140 142 auth, 141 143 headers, 142 144 query, 143 145 security: [ 144 146 { 145 - type: 'http', 147 + type: 'http' as const, 146 148 }, 147 149 ], 148 - }); 150 + }; 151 + await setAuthParams(options); 149 152 expect(auth).toHaveBeenCalled(); 150 - expect(headers.get('Authorization')).toBe('foo'); 153 + expect(options.headers.get('Authorization')).toBe('foo'); 151 154 expect(query).toEqual({}); 152 155 }); 153 156 154 157 it('sets first scheme only', async () => { 155 158 const auth = vi.fn().mockReturnValue('foo'); 156 - const headers = new Headers(); 159 + const headers = new HttpHeaders(); 157 160 const query: Record<any, unknown> = {}; 158 - await setAuthParams({ 161 + const options = { 159 162 auth, 160 163 headers, 161 164 query, 162 165 security: [ 163 166 { 164 167 name: 'baz', 165 - scheme: 'bearer', 166 - type: 'http', 168 + scheme: 'bearer' as const, 169 + type: 'http' as const, 167 170 }, 168 171 { 169 - in: 'query', 172 + in: 'query' as const, 170 173 name: 'baz', 171 - scheme: 'bearer', 172 - type: 'http', 174 + scheme: 'bearer' as const, 175 + type: 'http' as const, 173 176 }, 174 177 ], 175 - }); 178 + }; 179 + await setAuthParams(options); 176 180 expect(auth).toHaveBeenCalled(); 177 - expect(headers.get('baz')).toBe('Bearer foo'); 181 + expect(options.headers.get('baz')).toBe('Bearer foo'); 178 182 expect(Object.keys(query).length).toBe(0); 179 183 }); 180 184 ··· 185 189 } 186 190 return 'foo'; 187 191 }); 188 - const headers = new Headers(); 192 + const headers = new HttpHeaders(); 189 193 const query: Record<any, unknown> = {}; 190 194 await setAuthParams({ 191 195 auth, ··· 211 215 212 216 it('sets an API key in a cookie', async () => { 213 217 const auth = vi.fn().mockReturnValue('foo'); 214 - const headers = new Headers(); 218 + const headers = new HttpHeaders(); 215 219 const query: Record<any, unknown> = {}; 216 - await setAuthParams({ 220 + const options = { 217 221 auth, 218 222 headers, 219 223 query, 220 224 security: [ 221 225 { 222 - in: 'cookie', 226 + in: 'cookie' as const, 223 227 name: 'baz', 224 - type: 'apiKey', 228 + type: 'apiKey' as const, 225 229 }, 226 230 ], 227 - }); 231 + }; 232 + await setAuthParams(options); 228 233 expect(auth).toHaveBeenCalled(); 229 - expect(headers.get('Cookie')).toBe('baz=foo'); 234 + expect(options.headers.get('Cookie')).toBe('baz=foo'); 230 235 expect(query).toEqual({}); 231 236 }); 232 237 });
+9 -10
packages/openapi-ts/src/plugins/@hey-api/client-angular/bundle/utils.ts
··· 187 187 return; 188 188 }; 189 189 190 - export const setAuthParams = async ({ 191 - security, 192 - ...options 193 - }: Pick<Required<RequestOptions>, 'security'> & 194 - Pick<RequestOptions, 'auth' | 'query'> & { 195 - headers: HttpHeaders; 196 - }) => { 197 - for (const auth of security) { 190 + export const setAuthParams = async ( 191 + options: Pick<Required<RequestOptions>, 'security'> & 192 + Pick<RequestOptions, 'auth' | 'query'> & { 193 + headers: HttpHeaders; 194 + }, 195 + ) => { 196 + for (const auth of options.security) { 198 197 const token = await getAuthToken(auth, options.auth); 199 198 200 199 if (!token) { ··· 211 210 options.query[name] = token; 212 211 break; 213 212 case 'cookie': 214 - options.headers.append('Cookie', `${name}=${token}`); 213 + options.headers = options.headers.append('Cookie', `${name}=${token}`); 215 214 break; 216 215 case 'header': 217 216 default: 218 - options.headers.set(name, token); 217 + options.headers = options.headers.set(name, token); 219 218 break; 220 219 } 221 220