github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/internal/tpl/ts/stub.tstmpl (about)

     1  {{/*
     2  		This is a template block to generate a DTO struct
     3  */}}
     4  {{ define "dto" }}
     5  	// {{.Name}} is a data transfer object
     6  	{{- range .Comments -}}
     7  		// {{.}}
     8  	{{ end -}}
     9  	{{$extends := strEmptySlice }}
    10  	{{- range .Fields -}}
    11  		{{- if and .IsDTO .Embedded }}{{$extends = strAppend $extends .Name}}{{end}}
    12  	{{- end }}
    13  	{{- if gt (len $extends) 0 }}
    14  		export interface {{.Name}} extends {{strJoin $extends ", "}} {
    15  	{{- else }}
    16  		export interface {{.Name}} {
    17  	{{- end }}
    18  	{{- range .Fields -}}
    19  		{{- if .IsDTO }}
    20  			{{- if .Embedded }} {{continue}} {{end}}
    21  			{{index (strSplit (.GetTag "json") ",") 0}}{{ if .IsPtr}}?{{end}}: {{tsType .RType}}
    22  		{{- else }}
    23  			{{index (strSplit (.GetTag "json") ",") 0}}{{ if .IsPtr}}?{{end}}: {{tsType .RType}}
    24  		{{- end }}
    25  	{{- end }}
    26  	}
    27  {{- end }}
    28  
    29  {{/* Generate the DTO interfaces */}}
    30  // Code generated by RonyKIT Stub Generator (TypeScript); DO NOT EDIT.
    31  
    32  import useSWR, { SWRConfiguration } from 'swr'
    33  
    34  {{ range $dtoName, $dto := .DTOs -}}
    35  	{{ template "dto" $dto }}
    36  {{- end }}
    37  
    38  
    39  {{/* Generate the Stub Class */}}
    40  {{$serviceName := .Name}}
    41  export class {{$serviceName}}Stub {
    42  readonly serverURL: string ;
    43  
    44  constructor(serverURL: string) {
    45  this.serverURL = serverURL.replace(/\/$/, '');
    46  }
    47  
    48  {{/*
    49  			Generating the REST methods
    50  */}}
    51  {{- range .RESTs -}}
    52  	{{$methodName := .Name}}
    53  	{{- if ne $methodName "" }}
    54  		// @ts-ignore
    55  		async {{lowerCamelCase $methodName}}(req: {{.Request.Name}}, headers?: HeadersInit): Promise<{{.Response.Name}}> {
    56  		{{- if eq (toLower .Method) "get" }}
    57  			const keys = Object.keys(req);
    58  			const keyValuePairs = keys.map(key => {
    59  			return encodeURIComponent(key) + '=' + encodeURIComponent((req as any)[key]);
    60  			}).join('&');
    61  			const queryParams =  (keyValuePairs.length > 0) ? `?${keyValuePairs}`: ""
    62  			const url = `${this.serverURL}{{tsReplacePathParams .Path "req."}}${queryParams}`;
    63  			return fetch(url, {
    64  			method: "{{.Method}}",
    65  			headers: {
    66  			"Content-Type": "application/json",
    67  			...headers,
    68  			}
    69  			}).then((res: Response) => {
    70  			if (res.status !== 200) {
    71  			throw new Error("Failed to fetch the data");
    72  			}
    73  
    74  			return res.json()
    75  			})
    76  		{{- else }}
    77  			return fetch(this.serverURL + `{{tsReplacePathParams .Path "req."}}`, {
    78  			method: "{{.Method}}",
    79  			headers: {
    80  			"Content-Type": "application/json",
    81  			...headers,
    82  			},
    83  			body: JSON.stringify(req)
    84  			}).then((res: Response) => {
    85  			if (res.status !== 200) {
    86  			throw new Error("Failed to fetch the data");
    87  			}
    88  
    89  			return res.json()
    90  			})
    91  		{{- end }}
    92  		}
    93  	{{- end }}
    94  {{- end }}
    95  
    96  } // end of {{$serviceName}}Stub
    97  
    98  {{/* Generating React Hooks */}}
    99  {{- range .RESTs -}}
   100  {{$methodName := .Name}}
   101  {{- if ne $methodName "" }}
   102  	export function use{{$methodName}}(
   103  	stub: {{$serviceName}}Stub,
   104  	req: {{.Request.Name}},
   105  	reqHeader?: HeadersInit,
   106  	options?: Partial<SWRConfiguration<{{.Response.Name}}>>
   107  	) {
   108  	return useSWR(
   109  	[req, '{{$methodName}}'],
   110  	(req) => {
   111  	return stub.{{lowerCamelCase $methodName}}(req[0], reqHeader)
   112  	},
   113  	options
   114  	)
   115  	}
   116  {{- end}}
   117  {{- end}}