Skip to content

Commit 4a97508

Browse files
authored
feat(angular): support paramsSerializer (#2275)
1 parent 28b61a4 commit 4a97508

File tree

16 files changed

+641
-37
lines changed

16 files changed

+641
-37
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ node_modules
1010
mockServiceWorker.js
1111
yarn.lock
1212
.svelte-kit
13+
14+
# Angular build cache
15+
**/.angular/cache

docs/src/pages/reference/configuration/output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,7 @@ export const customFormUrlEncodedFn = <Body>(body: Body): URLSearchParams => {
23432343

23442344
Type: `String` or `Object`.
23452345

2346-
IMPORTANT: This is only valid when using `axios`.
2346+
IMPORTANT: This is only valid when using `axios` or `angular`.
23472347

23482348
Valid values: path of the paramsSerializer function or object with a path and name.
23492349

packages/core/src/generators/options.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const generateAxiosOptions = ({
3737
requestOptions,
3838
hasSignal,
3939
isVue,
40+
isAngular,
4041
paramsSerializer,
4142
paramsSerializerOptions,
4243
}: {
@@ -47,6 +48,7 @@ export const generateAxiosOptions = ({
4748
requestOptions?: object | boolean;
4849
hasSignal: boolean;
4950
isVue: boolean;
51+
isAngular: boolean;
5052
paramsSerializer?: GeneratorMutator;
5153
paramsSerializerOptions?: ParamsSerializerOptions;
5254
}) => {
@@ -107,6 +109,8 @@ export const generateAxiosOptions = ({
107109
if (queryParams) {
108110
if (isVue) {
109111
value += '\n params: {...unref(params), ...options?.params},';
112+
} else if (isAngular && paramsSerializer) {
113+
value += `\n params: ${paramsSerializer.name}({...params, ...options?.params}),`;
110114
} else {
111115
value += '\n params: {...params, ...options?.params},';
112116
}
@@ -117,7 +121,11 @@ export const generateAxiosOptions = ({
117121
}
118122
}
119123

120-
if (queryParams && (paramsSerializer || paramsSerializerOptions?.qs)) {
124+
if (
125+
!isAngular &&
126+
queryParams &&
127+
(paramsSerializer || paramsSerializerOptions?.qs)
128+
) {
121129
if (paramsSerializer) {
122130
value += `\n paramsSerializer: ${paramsSerializer.name},`;
123131
} else {
@@ -175,6 +183,7 @@ export const generateOptions = ({
175183
isExactOptionalPropertyTypes,
176184
hasSignal,
177185
isVue: isVue ?? false,
186+
isAngular: isAngular ?? false,
178187
paramsSerializer,
179188
paramsSerializerOptions,
180189
});

samples/angular-app/orval.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export default defineConfig({
1212
tsconfig: './tsconfig.app.json',
1313
clean: true,
1414
override: {
15+
paramsSerializer: {
16+
path: 'src/api/mutator/custom-params-serializer.ts',
17+
},
1518
operations: {
1619
listPets: {
1720
mutator: 'src/api/mutator/response-type.ts',

samples/angular-app/src/api/endpoints/pets/pets.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type {
2828
} from '../../model';
2929

3030
import listPetsMutator from '../../mutator/response-type';
31+
import paramsSerializerMutator from '../../mutator/custom-params-serializer';
3132

3233
interface HttpClientOptions {
3334
headers?: HttpHeaders | Record<string, string | string[]>;
@@ -80,7 +81,7 @@ export class PetsService {
8081
): Observable<any> {
8182
return this.http.get<TData>(`/v${version}/search`, {
8283
...options,
83-
params: { ...params, ...options?.params },
84+
params: paramsSerializerMutator({ ...params, ...options?.params }),
8485
});
8586
}
8687
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { HttpParams } from '@angular/common/http';
2+
3+
type AngularHttpParams =
4+
| HttpParams
5+
| Record<
6+
string,
7+
string | number | boolean | readonly (string | number | boolean)[]
8+
>
9+
| undefined;
10+
11+
export default function (params: Record<string, any>): AngularHttpParams {
12+
// do your implementation to transform the params
13+
14+
return params;
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Generated by orval v7.11.2 🍺
3+
* Do not edit manually.
4+
* Swagger Petstore
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
8+
export type CreatePetsBodyItem = {
9+
name: string;
10+
tag: string;
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Generated by orval v7.11.2 🍺
3+
* Do not edit manually.
4+
* Swagger Petstore
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
8+
export interface Error {
9+
code: number;
10+
message: string;
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Generated by orval v7.11.2 🍺
3+
* Do not edit manually.
4+
* Swagger Petstore
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
8+
export * from './createPetsBodyItem';
9+
export * from './error';
10+
export * from './listPetsParams';
11+
export * from './pet';
12+
export * from './pets';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Generated by orval v7.11.2 🍺
3+
* Do not edit manually.
4+
* Swagger Petstore
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
8+
export type ListPetsParams = {
9+
/**
10+
* How many items to return at one time (max 100)
11+
*/
12+
limit?: string;
13+
};

0 commit comments

Comments
 (0)