Skip to content

Commit

Permalink
feat: improve client generation (#35)
Browse files Browse the repository at this point in the history
- improve matching of response on status codes
- introduce runtime package for static code
- drop ? when no query parameters provided
- make code output more readable
  • Loading branch information
mnahkies authored Apr 15, 2023
1 parent 915456f commit 48a806a
Show file tree
Hide file tree
Showing 17 changed files with 8,152 additions and 17,993 deletions.
8,009 changes: 1,858 additions & 6,151 deletions integration-tests/typescript-angular/src/app/api.github.com.yaml/client.service.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class ApiClient {
private readonly config: ApiClientConfig
) {}

private headers(
private _headers(
headers: Record<string, string | undefined>
): Record<string, string> {
return Object.fromEntries(
Expand All @@ -36,7 +36,7 @@ export class ApiClient {
)
}

private queryParams(
private _queryParams(
queryParams: Record<
string,
boolean | number | string | string[] | undefined | null
Expand All @@ -55,69 +55,47 @@ export class ApiClient {
tags?: string[]
limit?: number
}): Observable<t_Pet[] | t_Error> {
const headers: Record<string, string | undefined> = {}

const queryParameters = { tags: p["tags"], limit: p["limit"] }
const params = this._queryParams({ tags: p["tags"], limit: p["limit"] })

return this.httpClient.request<any>("GET", this.config.basePath + `/pets`, {
params: this.queryParams(queryParameters),
headers: this.headers(headers),

params,
observe: "body",
reportProgress: false,
})
}

addPet(p: { requestBody: t_NewPet }): Observable<t_Pet | t_Error> {
const headers: Record<string, string | undefined> = {
"Content-Type": "application/json",
}

const queryParameters = {}
const headers = this._headers({ "Content-Type": "application/json" })
const body = p["requestBody"]

return this.httpClient.request<any>(
"POST",
this.config.basePath + `/pets`,
{
params: this.queryParams(queryParameters),
headers: this.headers(headers),
body: p["requestBody"],
headers,
body,
observe: "body",
reportProgress: false,
}
)
}

findPetById(p: { id: number }): Observable<t_Pet | t_Error> {
const headers: Record<string, string | undefined> = {}

const queryParameters = {}

return this.httpClient.request<any>(
"GET",
this.config.basePath + `/pets/${p["id"]}`,
{
params: this.queryParams(queryParameters),
headers: this.headers(headers),

observe: "body",
reportProgress: false,
}
)
}

deletePet(p: { id: number }): Observable<void | t_Error> {
const headers: Record<string, string | undefined> = {}

const queryParameters = {}

return this.httpClient.request<any>(
"DELETE",
this.config.basePath + `/pets/${p["id"]}`,
{
params: this.queryParams(queryParameters),
headers: this.headers(headers),

observe: "body",
reportProgress: false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class ApiClient {
private readonly config: ApiClientConfig
) {}

private headers(
private _headers(
headers: Record<string, string | undefined>
): Record<string, string> {
return Object.fromEntries(
Expand All @@ -36,7 +36,7 @@ export class ApiClient {
)
}

private queryParams(
private _queryParams(
queryParams: Record<
string,
boolean | number | string | string[] | undefined | null
Expand All @@ -55,14 +55,13 @@ export class ApiClient {
created?: string
status?: "incomplete" | "complete"
}): Observable<t_TodoList[]> {
const headers: Record<string, string | undefined> = {}

const queryParameters = { created: p["created"], status: p["status"] }
const params = this._queryParams({
created: p["created"],
status: p["status"],
})

return this.httpClient.request<any>("GET", this.config.basePath + `/list`, {
params: this.queryParams(queryParameters),
headers: this.headers(headers),

params,
observe: "body",
reportProgress: false,
})
Expand All @@ -71,17 +70,10 @@ export class ApiClient {
getTodoListById(p: {
listId: string
}): Observable<t_TodoList | t_Error | void> {
const headers: Record<string, string | undefined> = {}

const queryParameters = {}

return this.httpClient.request<any>(
"GET",
this.config.basePath + `/list/${p["listId"]}`,
{
params: this.queryParams(queryParameters),
headers: this.headers(headers),

observe: "body",
reportProgress: false,
}
Expand All @@ -92,37 +84,26 @@ export class ApiClient {
listId: string
requestBody: t_CreateUpdateTodoList
}): Observable<t_TodoList | t_Error | void> {
const headers: Record<string, string | undefined> = {
"Content-Type": "application/json",
}

const queryParameters = {}
const headers = this._headers({ "Content-Type": "application/json" })
const body = p["requestBody"]

return this.httpClient.request<any>(
"PUT",
this.config.basePath + `/list/${p["listId"]}`,
{
params: this.queryParams(queryParameters),
headers: this.headers(headers),
body: p["requestBody"],
headers,
body,
observe: "body",
reportProgress: false,
}
)
}

deleteTodoListById(p: { listId: string }): Observable<void | t_Error | void> {
const headers: Record<string, string | undefined> = {}

const queryParameters = {}

return this.httpClient.request<any>(
"DELETE",
this.config.basePath + `/list/${p["listId"]}`,
{
params: this.queryParams(queryParameters),
headers: this.headers(headers),

observe: "body",
reportProgress: false,
}
Expand Down
1 change: 1 addition & 0 deletions integration-tests/typescript-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"validate": "tsc -p ./tsconfig.json"
},
"dependencies": {
"@nahkies/typescript-fetch-runtime": "0.0.1",
"dotenv": "^16.0.3"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 48a806a

Please sign in to comment.