-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.ts
82 lines (70 loc) · 1.82 KB
/
functions.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
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
import { join } from 'https://deno.land/[email protected]/path/mod.ts'
import { PostmanItem, Route } from './types.ts'
import { unflatten } from 'npm:flat'
import { destr } from 'npm:destr'
export async function* recursiveReaddir(
path: string,
): AsyncGenerator<string, void> {
for await (const dirEntry of Deno.readDir(path)) {
if (dirEntry.isDirectory) {
yield* recursiveReaddir(join(path, dirEntry.name))
} else if (dirEntry.isFile) {
yield join(path, dirEntry.name)
}
}
}
export function wrap<T>(val: T | T[]) {
return Array.isArray(val) ? val : [val]
}
export function colonToVariable(route: string) {
return route.replace(/:([^/]+)/g, `{{$1}}`)
}
// export function toFolder(path: string) {
// return path.split('/').reduce((res, name) => {
// res[name] =
// return res
// }, {})
// }
export function fixFileName(file: string) {
return file
.replace(/\.\.\//g, '')
.replace('/etc/webapi.xml', '')
.replace('app/vendor/', 'vendor/')
.replace('app/app', 'app')
}
const customer_token: PostmanItem['request']['auth'] = {
type: 'bearer',
bearer: [
{
key: 'token',
type: 'string',
value: '{{customer_token}}',
},
],
}
export function parseRoute(route: Route) {
const url = `{{base_url}}/rest${colonToVariable(route.url)}`
const [host, ...path] = url.split('/')
if (route.auth !== 'anonymous') route.url += ` (${route.auth})`
const item: PostmanItem = {
name: route.url,
request: {
description: `${route.class}@${route.method}`,
method: route.type,
url: {
raw: url,
host: [host],
path,
},
},
}
if (route.data) {
item.request.body = {
mode: 'raw',
raw: JSON.stringify(unflatten(destr(`{ "${route.data}": "<insert_value_here>" }`)), null, 2),
options: { raw: { language: 'json' } },
}
}
if (route.auth === 'self') item.request.auth = customer_token
return item
}