-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstringifyVariables.ts
46 lines (41 loc) · 1.45 KB
/
stringifyVariables.ts
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
import { losslessParse, losslessStringify } from '../../lib'
import { Job } from './interfaces-1.0'
import { ActivatedJob } from './interfaces-grpc-1.0'
export function parseVariables<T extends { variables: string }, V = JSONDoc>(
input: T
): Omit<T, 'variables'> & { variables: V } {
return Object.assign({}, input, {
variables: losslessParse(input.variables || '{}') as V,
})
}
export function parseVariablesAndCustomHeadersToJSON<Variables, CustomHeaders>(
response: ActivatedJob,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputVariableDto: new (...args: any[]) => Readonly<Variables>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
customHeadersDto: new (...args: any[]) => Readonly<CustomHeaders>
): Job<Variables, CustomHeaders> {
return Object.assign({}, response, {
customHeaders: losslessParse(
response.customHeaders,
customHeadersDto
) as CustomHeaders,
variables: losslessParse(
response.variables,
inputVariableDto
) as Readonly<Variables>,
}) as Job<Variables, CustomHeaders>
}
export function stringifyVariables<
K,
T extends { variables: K extends JSONDoc ? K : K },
V extends T & { variables: string },
>(request: T): V {
const variables = request.variables || {}
const variablesString = losslessStringify(variables)
return Object.assign({}, request, { variables: variablesString }) as V
}
type JSON = string | number | boolean | JSON[] | JSONDoc[]
interface JSONDoc {
[key: string]: JSON
}