Skip to content

Commit f9dd1cd

Browse files
committed
feat: Refactor SDK methods to use direct function exports instead of class methods
- Updated the SDK generation to export functions for API operations (e.g., addPet, updatePet) instead of using static class methods. - Removed Angular service classes for Pet, Store, and User, simplifying the API interaction. - Enhanced the Angular resource plugin to support customizable class and method name builders for generated resources. - Adjusted method names in the generated Angular services to follow the new naming conventions. - Updated the configuration types to include optional builders for class and method names.
1 parent fd6326a commit f9dd1cd

File tree

7 files changed

+511
-491
lines changed

7 files changed

+511
-491
lines changed

examples/openapi-ts-angular/openapi-ts.config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ export default defineConfig({
1212
'@hey-api/client-angular',
1313
'@tanstack/angular-query-experimental',
1414
{
15-
// asClass: true,
15+
asClass: true,
1616
name: '@hey-api/angular-resource',
1717
},
1818
'@hey-api/schemas',
1919
{
20-
asClass: true,
20+
asClass: false,
21+
classNameBuilder(name) {
22+
return `${name}Service`;
23+
},
24+
methodNameBuilder(operation) {
25+
return String(operation.id);
26+
},
2127
name: '@hey-api/sdk',
2228
},
2329
{

examples/openapi-ts-angular/src/client/@hey-api/angular-resource.gen.ts

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@
33
import { Injectable, resource } from '@angular/core';
44

55
import type { Options } from '../sdk.gen';
6-
import { Pet, Store, User } from '../sdk.gen';
6+
import {
7+
addPet,
8+
createUser,
9+
createUsersWithListInput,
10+
deleteOrder,
11+
deletePet,
12+
deleteUser,
13+
findPetsByStatus,
14+
findPetsByTags,
15+
getInventory,
16+
getOrderById,
17+
getPetById,
18+
getUserByName,
19+
loginUser,
20+
logoutUser,
21+
placeOrder,
22+
updatePet,
23+
updatePetWithForm,
24+
updateUser,
25+
uploadFile,
26+
} from '../sdk.gen';
727
import type {
828
AddPetData,
929
CreateUserData,
@@ -29,16 +49,16 @@ import type {
2949
@Injectable({
3050
providedIn: 'root',
3151
})
32-
export class PetResource {
52+
export class PetServiceResources {
3353
/**
3454
* Add a new pet to the store.
3555
* Add a new pet to the store.
3656
*/
37-
public addPet<ThrowOnError extends boolean = false>(
57+
public addPetResource<ThrowOnError extends boolean = false>(
3858
options: Options<AddPetData, ThrowOnError>,
3959
) {
4060
return resource({
41-
loader: async ({ params }) => Pet.addPet(params),
61+
loader: async ({ params }) => addPet(params),
4262
params: () => options,
4363
});
4464
}
@@ -47,11 +67,11 @@ export class PetResource {
4767
* Update an existing pet.
4868
* Update an existing pet by Id.
4969
*/
50-
public updatePet<ThrowOnError extends boolean = false>(
70+
public updatePetResource<ThrowOnError extends boolean = false>(
5171
options: Options<UpdatePetData, ThrowOnError>,
5272
) {
5373
return resource({
54-
loader: async ({ params }) => Pet.updatePet(params),
74+
loader: async ({ params }) => updatePet(params),
5575
params: () => options,
5676
});
5777
}
@@ -60,11 +80,11 @@ export class PetResource {
6080
* Finds Pets by status.
6181
* Multiple status values can be provided with comma separated strings.
6282
*/
63-
public findPetsByStatus<ThrowOnError extends boolean = false>(
83+
public findPetsByStatusResource<ThrowOnError extends boolean = false>(
6484
options: Options<FindPetsByStatusData, ThrowOnError>,
6585
) {
6686
return resource({
67-
loader: async ({ params }) => Pet.findPetsByStatus(params),
87+
loader: async ({ params }) => findPetsByStatus(params),
6888
params: () => options,
6989
});
7090
}
@@ -73,11 +93,11 @@ export class PetResource {
7393
* Finds Pets by tags.
7494
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
7595
*/
76-
public findPetsByTags<ThrowOnError extends boolean = false>(
96+
public findPetsByTagsResource<ThrowOnError extends boolean = false>(
7797
options: Options<FindPetsByTagsData, ThrowOnError>,
7898
) {
7999
return resource({
80-
loader: async ({ params }) => Pet.findPetsByTags(params),
100+
loader: async ({ params }) => findPetsByTags(params),
81101
params: () => options,
82102
});
83103
}
@@ -86,11 +106,11 @@ export class PetResource {
86106
* Deletes a pet.
87107
* Delete a pet.
88108
*/
89-
public deletePet<ThrowOnError extends boolean = false>(
109+
public deletePetResource<ThrowOnError extends boolean = false>(
90110
options: Options<DeletePetData, ThrowOnError>,
91111
) {
92112
return resource({
93-
loader: async ({ params }) => Pet.deletePet(params),
113+
loader: async ({ params }) => deletePet(params),
94114
params: () => options,
95115
});
96116
}
@@ -99,11 +119,11 @@ export class PetResource {
99119
* Find pet by ID.
100120
* Returns a single pet.
101121
*/
102-
public getPetById<ThrowOnError extends boolean = false>(
122+
public getPetByIdResource<ThrowOnError extends boolean = false>(
103123
options: Options<GetPetByIdData, ThrowOnError>,
104124
) {
105125
return resource({
106-
loader: async ({ params }) => Pet.getPetById(params),
126+
loader: async ({ params }) => getPetById(params),
107127
params: () => options,
108128
});
109129
}
@@ -112,11 +132,11 @@ export class PetResource {
112132
* Updates a pet in the store with form data.
113133
* Updates a pet resource based on the form data.
114134
*/
115-
public updatePetWithForm<ThrowOnError extends boolean = false>(
135+
public updatePetWithFormResource<ThrowOnError extends boolean = false>(
116136
options: Options<UpdatePetWithFormData, ThrowOnError>,
117137
) {
118138
return resource({
119-
loader: async ({ params }) => Pet.updatePetWithForm(params),
139+
loader: async ({ params }) => updatePetWithForm(params),
120140
params: () => options,
121141
});
122142
}
@@ -125,11 +145,11 @@ export class PetResource {
125145
* Uploads an image.
126146
* Upload image of the pet.
127147
*/
128-
public uploadFile<ThrowOnError extends boolean = false>(
148+
public uploadFileResource<ThrowOnError extends boolean = false>(
129149
options: Options<UploadFileData, ThrowOnError>,
130150
) {
131151
return resource({
132-
loader: async ({ params }) => Pet.uploadFile(params),
152+
loader: async ({ params }) => uploadFile(params),
133153
params: () => options,
134154
});
135155
}
@@ -138,16 +158,16 @@ export class PetResource {
138158
@Injectable({
139159
providedIn: 'root',
140160
})
141-
export class StoreResource {
161+
export class StoreServiceResources {
142162
/**
143163
* Returns pet inventories by status.
144164
* Returns a map of status codes to quantities.
145165
*/
146-
public getInventory<ThrowOnError extends boolean = false>(
166+
public getInventoryResource<ThrowOnError extends boolean = false>(
147167
options?: Options<GetInventoryData, ThrowOnError>,
148168
) {
149169
return resource({
150-
loader: async ({ params }) => Store.getInventory(params),
170+
loader: async ({ params }) => getInventory(params),
151171
params: () => options,
152172
});
153173
}
@@ -156,11 +176,11 @@ export class StoreResource {
156176
* Place an order for a pet.
157177
* Place a new order in the store.
158178
*/
159-
public placeOrder<ThrowOnError extends boolean = false>(
179+
public placeOrderResource<ThrowOnError extends boolean = false>(
160180
options?: Options<PlaceOrderData, ThrowOnError>,
161181
) {
162182
return resource({
163-
loader: async ({ params }) => Store.placeOrder(params),
183+
loader: async ({ params }) => placeOrder(params),
164184
params: () => options,
165185
});
166186
}
@@ -169,11 +189,11 @@ export class StoreResource {
169189
* Delete purchase order by identifier.
170190
* For valid response try integer IDs with value < 1000. Anything above 1000 or non-integers will generate API errors.
171191
*/
172-
public deleteOrder<ThrowOnError extends boolean = false>(
192+
public deleteOrderResource<ThrowOnError extends boolean = false>(
173193
options: Options<DeleteOrderData, ThrowOnError>,
174194
) {
175195
return resource({
176-
loader: async ({ params }) => Store.deleteOrder(params),
196+
loader: async ({ params }) => deleteOrder(params),
177197
params: () => options,
178198
});
179199
}
@@ -182,11 +202,11 @@ export class StoreResource {
182202
* Find purchase order by ID.
183203
* For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions.
184204
*/
185-
public getOrderById<ThrowOnError extends boolean = false>(
205+
public getOrderByIdResource<ThrowOnError extends boolean = false>(
186206
options: Options<GetOrderByIdData, ThrowOnError>,
187207
) {
188208
return resource({
189-
loader: async ({ params }) => Store.getOrderById(params),
209+
loader: async ({ params }) => getOrderById(params),
190210
params: () => options,
191211
});
192212
}
@@ -195,16 +215,16 @@ export class StoreResource {
195215
@Injectable({
196216
providedIn: 'root',
197217
})
198-
export class UserResource {
218+
export class UserServiceResources {
199219
/**
200220
* Create user.
201221
* This can only be done by the logged in user.
202222
*/
203-
public createUser<ThrowOnError extends boolean = false>(
223+
public createUserResource<ThrowOnError extends boolean = false>(
204224
options?: Options<CreateUserData, ThrowOnError>,
205225
) {
206226
return resource({
207-
loader: async ({ params }) => User.createUser(params),
227+
loader: async ({ params }) => createUser(params),
208228
params: () => options,
209229
});
210230
}
@@ -213,11 +233,11 @@ export class UserResource {
213233
* Creates list of users with given input array.
214234
* Creates list of users with given input array.
215235
*/
216-
public createUsersWithListInput<ThrowOnError extends boolean = false>(
236+
public createUsersWithListInputResource<ThrowOnError extends boolean = false>(
217237
options?: Options<CreateUsersWithListInputData, ThrowOnError>,
218238
) {
219239
return resource({
220-
loader: async ({ params }) => User.createUsersWithListInput(params),
240+
loader: async ({ params }) => createUsersWithListInput(params),
221241
params: () => options,
222242
});
223243
}
@@ -226,11 +246,11 @@ export class UserResource {
226246
* Logs user into the system.
227247
* Log into the system.
228248
*/
229-
public loginUser<ThrowOnError extends boolean = false>(
249+
public loginUserResource<ThrowOnError extends boolean = false>(
230250
options?: Options<LoginUserData, ThrowOnError>,
231251
) {
232252
return resource({
233-
loader: async ({ params }) => User.loginUser(params),
253+
loader: async ({ params }) => loginUser(params),
234254
params: () => options,
235255
});
236256
}
@@ -239,11 +259,11 @@ export class UserResource {
239259
* Logs out current logged in user session.
240260
* Log user out of the system.
241261
*/
242-
public logoutUser<ThrowOnError extends boolean = false>(
262+
public logoutUserResource<ThrowOnError extends boolean = false>(
243263
options?: Options<LogoutUserData, ThrowOnError>,
244264
) {
245265
return resource({
246-
loader: async ({ params }) => User.logoutUser(params),
266+
loader: async ({ params }) => logoutUser(params),
247267
params: () => options,
248268
});
249269
}
@@ -252,11 +272,11 @@ export class UserResource {
252272
* Delete user resource.
253273
* This can only be done by the logged in user.
254274
*/
255-
public deleteUser<ThrowOnError extends boolean = false>(
275+
public deleteUserResource<ThrowOnError extends boolean = false>(
256276
options: Options<DeleteUserData, ThrowOnError>,
257277
) {
258278
return resource({
259-
loader: async ({ params }) => User.deleteUser(params),
279+
loader: async ({ params }) => deleteUser(params),
260280
params: () => options,
261281
});
262282
}
@@ -265,11 +285,11 @@ export class UserResource {
265285
* Get user by user name.
266286
* Get user detail based on username.
267287
*/
268-
public getUserByName<ThrowOnError extends boolean = false>(
288+
public getUserByNameResource<ThrowOnError extends boolean = false>(
269289
options: Options<GetUserByNameData, ThrowOnError>,
270290
) {
271291
return resource({
272-
loader: async ({ params }) => User.getUserByName(params),
292+
loader: async ({ params }) => getUserByName(params),
273293
params: () => options,
274294
});
275295
}
@@ -278,11 +298,11 @@ export class UserResource {
278298
* Update user resource.
279299
* This can only be done by the logged in user.
280300
*/
281-
public updateUser<ThrowOnError extends boolean = false>(
301+
public updateUserResource<ThrowOnError extends boolean = false>(
282302
options: Options<UpdateUserData, ThrowOnError>,
283303
) {
284304
return resource({
285-
loader: async ({ params }) => User.updateUser(params),
305+
loader: async ({ params }) => updateUser(params),
286306
params: () => options,
287307
});
288308
}

0 commit comments

Comments
 (0)