forked from albertodeago/curl-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-body.ts
More file actions
31 lines (27 loc) · 817 Bytes
/
json-body.ts
File metadata and controls
31 lines (27 loc) · 817 Bytes
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
export interface CurlJsonBody {
/**
* JSON body type for JSON objects.
*/
type: "json",
/**
* The content of the body.
*/
content: Record<string | number | symbol, unknown>,
}
export function isCurlJsonBody(body: unknown): body is CurlJsonBody {
return typeof body === "object" && body !== null && "type" in body && (body as {
[key: string]: unknown
}).type === "json" && "content" in body;
}
export function jsonContentToString(content: Record<string | number | symbol, unknown>): string {
return JSON.stringify(content).replace(
/([\\'])/g,
"\\$1"
);
}
export function jsonBodyToString(body: CurlJsonBody): string {
return jsonContentToString(body.content);
}
export function jsonBodyToCommand(body: CurlJsonBody): string {
return `-d '${jsonBodyToString(body)}'`;
}