Skip to content

Commit 303498a

Browse files
committed
feat: 🎸 cli - adds optional query params
1 parent da8ca36 commit 303498a

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

‎packages/cli/src/codegen/openapi/generator.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
138138
};
139139

140140
static generateRequestInstanceType(
141-
{ id, path: endpoint }: { id: string; path: string },
141+
{ id, path: endpoint, queryParamsRequired }: { id: string; path: string; queryParamsRequired?: boolean },
142142
types: Record<string, string>,
143143
) {
144144
const Response = types[`${createTypeBaseName(id)}ResponseType`]
@@ -150,11 +150,19 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
150150
? `${createTypeBaseName(id)}QueryParams`
151151
: undefined;
152152

153-
return `Request<${Response}, ${Payload}, ${QueryParams}, ${LocalError}, "${endpoint}", Client>`;
153+
const QueryParamsGeneric =
154+
QueryParams && queryParamsRequired === false ? `${QueryParams} | undefined` : QueryParams;
155+
156+
return `Request<${Response}, ${Payload}, ${QueryParamsGeneric}, ${LocalError}, "${endpoint}", Client>`;
154157
}
155158

156159
static generateHyperFetchRequest(
157-
{ id, path: relPath, method }: { id: string; path: string; method: string },
160+
{
161+
id,
162+
path: relPath,
163+
method,
164+
queryParamsRequired,
165+
}: { id: string; path: string; method: string; queryParamsRequired?: boolean },
158166
types: Record<string, string>,
159167
) {
160168
const Response = types[`${createTypeBaseName(id)}ResponseType`]
@@ -186,7 +194,8 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
186194
addToGenericType("error", LocalError);
187195
}
188196
if (QueryParams) {
189-
addToGenericType("query", QueryParams);
197+
const key = queryParamsRequired === false ? "queryParams?" : "queryParams";
198+
addToGenericType(key, QueryParams);
190199
}
191200

192201
if (genericType) {
@@ -265,6 +274,14 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
265274

266275
const responseType = !lodash.isEmpty(responseTypePaths) ? responseTypePaths.join(" | ") : "any";
267276
const errorType = !lodash.isEmpty(errorTypePaths) ? errorTypePaths.join(" | ") : "undefined";
277+
const queryParamsRequired = Array.isArray(operation.parameters)
278+
? operation.parameters.every((p) => {
279+
if ("in" in p && p.in === "query" && p.required === false) {
280+
return true;
281+
}
282+
return false;
283+
})
284+
: undefined;
268285

269286
return {
270287
id: normalizedOperationId,
@@ -275,6 +292,7 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
275292
responseType,
276293
path: adjustPathParamsFormat(relPath),
277294
method: method ? method.toUpperCase() : HttpMethod.GET,
295+
queryParamsRequired,
278296
};
279297
}
280298

0 commit comments

Comments
 (0)