-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathtemplate.go
More file actions
589 lines (517 loc) · 16.9 KB
/
template.go
File metadata and controls
589 lines (517 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package generator
import (
"bytes"
"fmt"
"net/url"
"regexp"
"strings"
"text/template"
log "github.com/sirupsen/logrus"
"github.com/Masterminds/sprig"
"github.com/iancoleman/strcase"
"github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts/data"
"github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts/registry"
)
const tmpl = `
{{define "dependencies"}}
{{range .}}import * as {{.ModuleIdentifier}} from "{{.SourceFile}}"
{{end}}{{end}}
{{define "enums"}}
{{range .}}export enum {{.Name}} {
{{- range .Values}}
{{.}} = "{{.}}",
{{- end}}
}
{{end}}{{end}}
{{define "messages"}}{{range .}}
{{- if .HasOneOfFields}}
type Base{{.Name}} = {
{{- range .NonOneOfFields}}
{{fieldName .Name}}?: {{tsType .}}
{{- end}}
}
export type {{.Name}} = Base{{.Name}}
{{range $groupId, $fields := .OneOfFieldsGroups}} & OneOf<{ {{range $index, $field := $fields}}{{fieldName $field.Name}}: {{tsType $field}}{{if (lt (add $index 1) (len $fields))}}; {{end}}{{end}} }>
{{end}}
{{- else -}}
export type {{.Name}} = {
{{- range .Fields}}
{{fieldName .Name}}?: {{tsType .}}
{{- end}}
}
{{end}}
{{end}}{{end}}
{{define "services"}}{{range .}}export class {{.Name}} {
{{- range .Methods}}
{{- if .ServerStreaming }}
static {{.Name}}(req: {{tsType .Input}}, entityNotifier?: fm.NotifyStreamEntityArrival<{{tsType .Output}}>, initReq?: fm.InitReq): Promise<void> {
return fm.fetchStreamingRequest<{{tsType .Input}}, {{tsType .Output}}>(` + "`{{renderURL .}}`" + `, entityNotifier, {...initReq, {{buildInitReq .}}})
}
{{- else }}
static {{.Name}}(req: {{tsType .Input}}, initReq?: fm.InitReq): Promise<{{tsType .Output}}> {
return fm.fetchReq<{{tsType .Input}}, {{tsType .Output}}>(` + "`{{renderURL .}}`" + `, {...initReq, {{buildInitReq .}}})
}
{{- end}}
{{- end}}
}
{{end}}{{end}}
{{- if not .EnableStylingCheck}}
/* eslint-disable */
// @ts-nocheck
{{- end}}
/*
* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
*/
{{if .Dependencies}}{{- include "dependencies" .StableDependencies -}}{{end}}
{{- if .NeedsOneOfSupport}}
type Absent<T, K extends keyof T> = { [k in Exclude<keyof T, K>]?: undefined };
type OneOf<T> =
| { [k in keyof T]?: undefined }
| (
keyof T extends infer K ?
(K extends string & keyof T ? { [k in K]: T[K] } & Absent<T, K>
: never)
: never);
{{end}}
{{- if .Enums}}{{include "enums" .Enums}}{{end}}
{{- if .Messages}}{{include "messages" .Messages}}{{end}}
{{- if .Services}}{{include "services" .Services}}{{end}}
`
const fetchTmpl = `
{{- if not .EnableStylingCheck}}
/* eslint-disable */
// @ts-nocheck
{{- end}}
/*
* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
*/
/**
* base64 encoder and decoder
* Copied and adapted from https://github.com/protobufjs/protobuf.js/blob/master/lib/base64/index.js
*/
// Base64 encoding table
const b64 = new Array(64);
// Base64 decoding table
const s64 = new Array(123);
// 65..90, 97..122, 48..57, 43, 47
for (let i = 0; i < 64;)
s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
export function b64Encode(buffer: Uint8Array, start: number, end: number): string {
let parts: string[] = null;
const chunk = [];
let i = 0, // output index
j = 0, // goto index
t; // temporary
while (start < end) {
const b = buffer[start++];
switch (j) {
case 0:
chunk[i++] = b64[b >> 2];
t = (b & 3) << 4;
j = 1;
break;
case 1:
chunk[i++] = b64[t | b >> 4];
t = (b & 15) << 2;
j = 2;
break;
case 2:
chunk[i++] = b64[t | b >> 6];
chunk[i++] = b64[b & 63];
j = 0;
break;
}
if (i > 8191) {
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
i = 0;
}
}
if (j) {
chunk[i++] = b64[t];
chunk[i++] = 61;
if (j === 1)
chunk[i++] = 61;
}
if (parts) {
if (i)
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
return parts.join("");
}
return String.fromCharCode.apply(String, chunk.slice(0, i));
}
const invalidEncoding = "invalid encoding";
export function b64Decode(s: string): Uint8Array {
const buffer = [];
let offset = 0;
let j = 0, // goto index
t; // temporary
for (let i = 0; i < s.length;) {
let c = s.charCodeAt(i++);
if (c === 61 && j > 1)
break;
if ((c = s64[c]) === undefined)
throw Error(invalidEncoding);
switch (j) {
case 0:
t = c;
j = 1;
break;
case 1:
buffer[offset++] = t << 2 | (c & 48) >> 4;
t = c;
j = 2;
break;
case 2:
buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
t = c;
j = 3;
break;
case 3:
buffer[offset++] = (t & 3) << 6 | c;
j = 0;
break;
}
}
if (j === 1)
throw Error(invalidEncoding);
return new Uint8Array(buffer);
}
function b64Test(s: string): boolean {
return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(s);
}
export interface InitReq extends RequestInit {
pathPrefix?: string
}
export function replacer(key: any, value: any): any {
if(value && value.constructor === Uint8Array) {
return b64Encode(value, 0, value.length);
}
return value;
}
export function fetchReq<I, O>(path: string, init?: InitReq): Promise<O> {
const {pathPrefix, ...req} = init || {}
const url = pathPrefix ? ` + "`${pathPrefix}${path}`" + ` : path
return fetch(url, req).then(r => r.json().then((body: O) => {
if (!r.ok) { throw body; }
return body;
})) as Promise<O>
}
// NotifyStreamEntityArrival is a callback that will be called on streaming entity arrival
export type NotifyStreamEntityArrival<T> = (resp: T) => void
/**
* fetchStreamingRequest is able to handle grpc-gateway server side streaming call
* it takes NotifyStreamEntityArrival that lets users respond to entity arrival during the call
* all entities will be returned as an array after the call finishes.
**/
export async function fetchStreamingRequest<S, R>(path: string, callback?: NotifyStreamEntityArrival<R>, init?: InitReq) {
const {pathPrefix, ...req} = init || {}
const url = pathPrefix ?` + "`${pathPrefix}${path}`" + ` : path
const result = await fetch(url, req)
// needs to use the .ok to check the status of HTTP status code
// http other than 200 will not throw an error, instead the .ok will become false.
// see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#
if (!result.ok) {
const resp = await result.json()
const errMsg = resp.error && resp.error.message ? resp.error.message : ""
throw new Error(errMsg)
}
if (!result.body) {
throw new Error("response doesnt have a body")
}
await result.body
.pipeThrough(new TextDecoderStream())
.pipeThrough<R>(getNewLineDelimitedJSONDecodingStream<R>())
.pipeTo(getNotifyEntityArrivalSink((e: R) => {
if (callback) {
callback(e)
}
}))
// wait for the streaming to finish and return the success respond
return
}
/**
* JSONStringStreamController represents the transform controller that's able to transform the incoming
* new line delimited json content stream into entities and able to push the entity to the down stream
*/
interface JSONStringStreamController<T> extends TransformStreamDefaultController {
buf?: string
pos?: number
enqueue: (s: T) => void
}
/**
* getNewLineDelimitedJSONDecodingStream returns a TransformStream that's able to handle new line delimited json stream content into parsed entities
*/
function getNewLineDelimitedJSONDecodingStream<T>(): TransformStream<string, T> {
return new TransformStream({
start(controller: JSONStringStreamController<T>) {
controller.buf = ''
controller.pos = 0
},
transform(chunk: string, controller: JSONStringStreamController<T>) {
if (controller.buf === undefined) {
controller.buf = ''
}
if (controller.pos === undefined) {
controller.pos = 0
}
controller.buf += chunk
while (controller.pos < controller.buf.length) {
if (controller.buf[controller.pos] === '\n') {
const line = controller.buf.substring(0, controller.pos)
const response = JSON.parse(line)
controller.enqueue(response.result)
controller.buf = controller.buf.substring(controller.pos + 1)
controller.pos = 0
} else {
++controller.pos
}
}
}
})
}
/**
* getNotifyEntityArrivalSink takes the NotifyStreamEntityArrival callback and return
* a sink that will call the callback on entity arrival
* @param notifyCallback
*/
function getNotifyEntityArrivalSink<T>(notifyCallback: NotifyStreamEntityArrival<T>) {
return new WritableStream<T>({
write(entity: T) {
notifyCallback(entity)
}
})
}
type Primitive = string | boolean | number;
type RequestPayload = Record<string, unknown>;
type FlattenedRequestPayload = Record<string, Primitive | Array<Primitive>>;
/**
* Checks if given value is a plain object
* Logic copied and adapted from below source:
* https://github.com/char0n/ramda-adjunct/blob/master/src/isPlainObj.js
* @param {unknown} value
* @return {boolean}
*/
function isPlainObject(value: unknown): boolean {
const isObject =
Object.prototype.toString.call(value).slice(8, -1) === "Object";
const isObjLike = value !== null && isObject;
if (!isObjLike || !isObject) {
return false;
}
const proto = Object.getPrototypeOf(value);
const hasObjectConstructor =
typeof proto === "object" &&
proto.constructor === Object.prototype.constructor;
return hasObjectConstructor;
}
/**
* Checks if given value is of a primitive type
* @param {unknown} value
* @return {boolean}
*/
function isPrimitive(value: unknown): boolean {
return ["string", "number", "boolean"].some(t => typeof value === t);
}
/**
* Checks if given primitive is zero-value
* @param {Primitive} value
* @return {boolean}
*/
function isZeroValuePrimitive(value: Primitive): boolean {
return value === false || value === 0 || value === "";
}
/**
* Flattens a deeply nested request payload and returns an object
* with only primitive values and non-empty array of primitive values
* as per https://github.com/googleapis/googleapis/blob/master/google/api/http.proto
* @param {RequestPayload} requestPayload
* @param {String} path
* @return {FlattenedRequestPayload>}
*/
function flattenRequestPayload<T extends RequestPayload>(
requestPayload: T,
path: string = ""
): FlattenedRequestPayload {
return Object.keys(requestPayload).reduce(
(acc: T, key: string): T => {
const value = requestPayload[key];
const newPath = path ? [path, key].join(".") : key;
const isNonEmptyPrimitiveArray =
Array.isArray(value) &&
value.every(v => isPrimitive(v)) &&
value.length > 0;
const isNonZeroValuePrimitive =
isPrimitive(value) && !isZeroValuePrimitive(value as Primitive);
let objectToMerge = {};
if (isPlainObject(value)) {
objectToMerge = flattenRequestPayload(value as RequestPayload, newPath);
} else if (isNonZeroValuePrimitive || isNonEmptyPrimitiveArray) {
objectToMerge = { [newPath]: value };
}
return { ...acc, ...objectToMerge };
},
{} as T
) as FlattenedRequestPayload;
}
/**
* Renders a deeply nested request payload into a string of URL search
* parameters by first flattening the request payload and then removing keys
* which are already present in the URL path.
* @param {RequestPayload} requestPayload
* @param {string[]} urlPathParams
* @return {string}
*/
export function renderURLSearchParams<T extends RequestPayload>(
requestPayload: T,
urlPathParams: string[] = []
): string {
const flattenedRequestPayload = flattenRequestPayload(requestPayload);
const urlSearchParams = Object.keys(flattenedRequestPayload).reduce(
(acc: string[][], key: string): string[][] => {
// key should not be present in the url path as a parameter
const value = flattenedRequestPayload[key];
if (urlPathParams.find(f => f === key)) {
return acc;
}
return Array.isArray(value)
? [...acc, ...value.map(m => [key, m.toString()])]
: (acc = [...acc, [key, value.toString()]]);
},
[] as string[][]
);
return new URLSearchParams(urlSearchParams).toString();
}
`
// GetTemplate gets the templates to for the typescript file
func GetTemplate(r *registry.Registry) *template.Template {
t := template.New("file")
t = t.Funcs(sprig.TxtFuncMap())
t = t.Funcs(template.FuncMap{
"include": include(t),
"tsType": func(fieldType data.Type) string {
return tsType(r, fieldType)
},
"renderURL": renderURL(r),
"buildInitReq": buildInitReq,
"fieldName": fieldName(r),
})
t = template.Must(t.Parse(tmpl))
return t
}
func fieldName(r *registry.Registry) func(name string) string {
return func(name string) string {
if r.UseProtoNames {
return name
}
return strcase.ToLowerCamel(name)
}
}
func renderURL(r *registry.Registry) func(method data.Method) string {
fieldNameFn := fieldName(r)
return func(method data.Method) string {
methodURL := method.URL
// capture fields like {abc} or {abc=def/ghi/*}.
// discard the pattern after the equal sign.
// relies on greedy capture within first part
// to avoid matching 2nd group when no equal sign present.
reg := regexp.MustCompile("{([^=}]+)=?([^}]+)?}")
matches := reg.FindAllStringSubmatch(methodURL, -1)
fieldsInPath := make([]string, 0, len(matches))
if len(matches) > 0 {
log.Debugf("url matches %v", matches)
for _, m := range matches {
expToReplace := m[0]
// convert foo_bar.baz_qux to fieldName `fooBar.bazQux`, part `${req["fooBar"]["bazQux"]}`
subFields := strings.Split(m[1], ".")
var subFieldNames, partNames []string
for _, subField := range subFields {
subFieldName := fieldNameFn(subField)
subFieldNames = append(subFieldNames, subFieldName)
partNames = append(partNames, fmt.Sprintf(`["%s"]`, subFieldName))
}
fieldName := strings.Join(subFieldNames, ".")
part := fmt.Sprintf(`${req%s}`, strings.Join(partNames, "?."))
methodURL = strings.ReplaceAll(methodURL, expToReplace, part)
fieldsInPath = append(fieldsInPath, fmt.Sprintf(`"%s"`, fieldName))
}
}
urlPathParams := fmt.Sprintf("[%s]", strings.Join(fieldsInPath, ", "))
if !method.ClientStreaming && method.HTTPMethod == "GET" {
// parse the url to check for query string
parsedURL, err := url.Parse(methodURL)
if err != nil {
return methodURL
}
renderURLSearchParamsFn := fmt.Sprintf("${fm.renderURLSearchParams(req, %s)}", urlPathParams)
// prepend "&" if query string is present otherwise prepend "?"
// trim leading "&" if present before prepending it
if parsedURL.RawQuery != "" {
methodURL = strings.TrimRight(methodURL, "&") + "&" + renderURLSearchParamsFn
} else {
methodURL += "?" + renderURLSearchParamsFn
}
}
return methodURL
}
}
func buildInitReq(method data.Method) string {
httpMethod := method.HTTPMethod
m := `method: "` + httpMethod + `"`
fields := []string{m}
if method.HTTPRequestBody == nil || *method.HTTPRequestBody == "*" {
fields = append(fields, "body: JSON.stringify(req, fm.replacer)")
} else if *method.HTTPRequestBody != "" {
fields = append(fields, `body: JSON.stringify(req["`+*method.HTTPRequestBody+`"], fm.replacer)`)
}
return strings.Join(fields, ", ")
}
// GetFetchModuleTemplate returns the go template for fetch module
func GetFetchModuleTemplate() *template.Template {
t := template.New("fetch")
return template.Must(t.Parse(fetchTmpl))
}
// include is the include template functions copied from
// copied from: https://github.com/helm/helm/blob/8648ccf5d35d682dcd5f7a9c2082f0aaf071e817/pkg/engine/engine.go#L147-L154
func include(t *template.Template) func(name string, data interface{}) (string, error) {
return func(name string, data interface{}) (string, error) {
buf := bytes.NewBufferString("")
if err := t.ExecuteTemplate(buf, name, data); err != nil {
return "", err
}
return buf.String(), nil
}
}
func tsType(r *registry.Registry, fieldType data.Type) string {
info := fieldType.GetType()
typeInfo, ok := r.Types[info.Type]
if ok && typeInfo.IsMapEntry {
keyType := tsType(r, typeInfo.KeyType)
valueType := tsType(r, typeInfo.ValueType)
return fmt.Sprintf("{[key: %s]: %s}", keyType, valueType)
}
typeStr := ""
if strings.Index(info.Type, ".") != 0 {
typeStr = mapScalaType(info.Type)
} else if !info.IsExternal {
typeStr = typeInfo.PackageIdentifier
} else {
typeStr = data.GetModuleName(typeInfo.Package, typeInfo.File) + "." + typeInfo.PackageIdentifier
}
if info.IsRepeated {
typeStr += "[]"
}
return typeStr
}
func mapScalaType(protoType string) string {
switch protoType {
case "uint64", "sint64", "int64", "fixed64", "sfixed64", "string":
return "string"
case "float", "double", "int32", "sint32", "uint32", "fixed32", "sfixed32":
return "number"
case "bool":
return "boolean"
case "bytes":
return "Uint8Array"
}
return ""
}