Skip to content

Commit 7861b22

Browse files
authored
Merge pull request #2 from kpoelhekke/feature/export-more
feat: export Params types and fetch function
2 parents 8ecbdde + 60b643a commit 7861b22

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mock-backend
22
dist
33
.rpt2_cache
4+
.idea
45

56
### macOS ###
67
*.DS_Store

src/generateHooks.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export const createHook = ({
308308

309309
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
310310
output += `
311-
const ${fetchName} = async () => {
311+
export const ${fetchName} = async () => {
312312
const result = await api.${verb}<${responseTypes}>(\`${route}\`);
313313
return result.data;
314314
}
@@ -318,12 +318,12 @@ export const createHook = ({
318318
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
319319
const config = isUpdateRequest ? 'undefined,{params}' : '{params}';
320320
output += `
321-
type ${componentName}Params = {
321+
export type ${componentName}Params = {
322322
${paramsTypes}
323323
${queryParamsType};
324324
}
325325
326-
const ${fetchName} = async (props:${componentName}Params) => {
326+
export const ${fetchName} = async (props:${componentName}Params) => {
327327
const {${paramsInPath.join(', ')}, ...params} = props
328328
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
329329
return result.data;
@@ -332,11 +332,11 @@ export const createHook = ({
332332

333333
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
334334
output += `
335-
type ${componentName}Params = {
335+
export type ${componentName}Params = {
336336
${paramsTypes}
337337
}
338338
339-
const ${fetchName} = async (props: ${componentName}Params ) => {
339+
export const ${fetchName} = async (props: ${componentName}Params ) => {
340340
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`);
341341
return result.data;
342342
}
@@ -346,11 +346,11 @@ export const createHook = ({
346346
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
347347
const config = isUpdateRequest ? 'null,{params}' : '{params}';
348348
output += `
349-
type ${componentName}Params = {
349+
export type ${componentName}Params = {
350350
${queryParamsType}
351351
}
352352
353-
const ${fetchName} = async (params: ${componentName}Params) => {
353+
export const ${fetchName} = async (params: ${componentName}Params) => {
354354
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
355355
return result.data;
356356
}
@@ -359,11 +359,11 @@ export const createHook = ({
359359
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
360360
const config = isUpdateRequest ? 'null,{headers, params: queryParams}' : '{headers, params: queryParams}';
361361
output += `
362-
type ${componentName}Params = {
362+
export type ${componentName}Params = {
363363
${headerParam}
364364
${queryParamsType}
365365
}
366-
const ${fetchName} = async (props: ${componentName}Params) => {
366+
export const ${fetchName} = async (props: ${componentName}Params) => {
367367
const headers = {${generateProps(header)}}
368368
const queryParams = {${generateProps(queryParams)}}
369369
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
@@ -373,11 +373,11 @@ export const createHook = ({
373373
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
374374
const config = isUpdateRequest ? 'null,{headers}' : '{headers}';
375375
output += `
376-
type ${componentName}Params = {
376+
export type ${componentName}Params = {
377377
${headerParam}
378378
};
379379
380-
const ${fetchName} = async (headers: ${componentName}Params) => {
380+
export const ${fetchName} = async (headers: ${componentName}Params) => {
381381
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config});
382382
return result.data;
383383
}
@@ -386,9 +386,9 @@ export const createHook = ({
386386

387387
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
388388
output += `
389-
type ${componentName}Params = ${requestBodyComponent}
389+
export type ${componentName}Params = ${requestBodyComponent}
390390
391-
const ${fetchName} = async (body: ${componentName}Params) => {
391+
export const ${fetchName} = async (body: ${componentName}Params) => {
392392
const result = await api.${verb}<${responseTypes}>(\`${route}\`, body)
393393
return result.data
394394
}
@@ -397,11 +397,11 @@ export const createHook = ({
397397

398398
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
399399
output += `
400-
type ${componentName}Params = ${body} & {
400+
export type ${componentName}Params = ${body} & {
401401
${queryParamsType}
402402
}
403403
404-
const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
404+
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
405405
${generateBodyProps()}
406406
const params = {${generateProps(queryParams)}}
407407
const result = await api.${verb}<${responseTypes}>(\`${route}\`, body, {params})
@@ -412,11 +412,11 @@ export const createHook = ({
412412

413413
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
414414
output += `
415-
type ${componentName}Params = ${body} & {
415+
export type ${componentName}Params = ${body} & {
416416
${headerParam}
417417
};
418418
419-
const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
419+
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
420420
${generateBodyProps()}
421421
const headers = {${generateProps(header)}}
422422
const result = await api.${verb}<${responseTypes}>(\`${route}\`, body, {headers})
@@ -427,11 +427,11 @@ export const createHook = ({
427427

428428
if (requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
429429
output += `
430-
type ${componentName}Params = ${body} & {
430+
export type ${componentName}Params = ${body} & {
431431
${headerParam}
432432
${paramsTypes}
433433
};
434-
const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
434+
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
435435
${generateBodyProps()}
436436
const headers = {${generateProps(header)}}
437437
const params = {${generateProps(queryParams)}}
@@ -443,12 +443,12 @@ export const createHook = ({
443443

444444
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
445445
output += `
446-
type ${componentName}Params = ${body} & {
446+
export type ${componentName}Params = ${body} & {
447447
${headerParam}
448448
${paramsTypes}
449449
};
450450
// TEST1
451-
const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
451+
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
452452
${generateBodyProps()}
453453
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`, body)
454454
return result.data
@@ -467,12 +467,12 @@ export const createHook = ({
467467
if (!requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
468468
const config = isUpdateRequest ? 'null,{headers}' : '{headers}';
469469
output += `
470-
type ${componentName}Params = {
470+
export type ${componentName}Params = {
471471
${headerParam}
472472
${paramsTypes}
473473
};
474474
475-
const ${fetchName} = async (props: ${componentName}Params) => {
475+
export const ${fetchName} = async (props: ${componentName}Params) => {
476476
const headers = {${generateProps(header)}}
477477
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`, ${config})
478478
return result.data

0 commit comments

Comments
 (0)