-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathindex.mjs
208 lines (188 loc) · 5.96 KB
/
index.mjs
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
//Imports
import { faker } from "@faker-js/faker"
import { Client as Gmap } from "@googlemaps/google-maps-services-js"
import axios from "axios"
import fs from "fs/promises"
import paths from "path"
import rss from "rss-parser"
import urls from "url"
//Mocked state
let mocked = false
//Mocking
export default async function({graphql, rest}) {
//Check if already mocked
if (mocked)
return {graphql, rest}
mocked = true
process.env.METRICS_MOCKED = true
console.debug("metrics/compute/mocks > mocking")
//Load mocks
const __mocks = paths.join(paths.dirname(urls.fileURLToPath(import.meta.url)))
const mock = async ({directory, mocks}) => {
for (const entry of await fs.readdir(directory)) {
if ((await fs.lstat(paths.join(directory, entry))).isDirectory()) {
if (!mocks[entry])
mocks[entry] = {}
await mock({directory: paths.join(directory, entry), mocks: mocks[entry]})
}
else {
mocks[entry.replace(/[.]mjs$/, "")] = (await import(urls.pathToFileURL(paths.join(directory, entry)).href)).default
}
}
return mocks
}
const mocks = await mock({directory: paths.join(__mocks, "api"), mocks: {}})
//GraphQL API mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking graphql api")
const unmocked = graphql
//Mocked
graphql = new Proxy(unmocked, {
apply(target, that, args) {
//Arguments
const [query] = args
const login = query.match(/login: "(?<login>.*?)"/)?.groups?.login ?? faker.internet.userName()
//Search for mocked query
for (const mocked of Object.keys(mocks.github.graphql)) {
if (new RegExp(`^query ${mocked.replace(/([.]\w)/g, (_, g) => g.toLocaleUpperCase().substring(1)).replace(/^(\w)/g, (_, g) => g.toLocaleUpperCase())} `).test(query))
return mocks.github.graphql[mocked]({faker, query, login})
}
//Unmocked call
return target(...args)
},
})
}
//Rest API mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking rest api")
const unmocked = rest
//Mocked
rest = new Proxy(unmocked, {
get(target, section) {
if (Reflect.has(mocks.github.rest, section)) {
return new Proxy(target[section], {
get(target, property) {
if (mocks.github.rest?.[section]?.[property]) {
return async function() {
console.debug(`metrics/mocking > rest.${section}.${property}`)
return mocks.github.rest[section][property]({faker}, target, null, arguments)
}
}
return Reflect.get(target, property)
},
})
}
return Reflect.get(target, section)
},
})
}
//Axios mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking axios")
const unmocked = {get: axios.get, post: axios.post}
//Mocked post requests
axios.post = new Proxy(unmocked.post, {
apply(target, that, args) {
//Arguments
const [url, body] = args
//Search for mocked request
for (const service of Object.keys(mocks.axios.post)) {
const mocked = mocks.axios.post[service]({faker, url, body})
if (mocked)
return mocked
}
//Unmocked call
return target(...args)
},
})
//Mocked get requests
axios.get = new Proxy(unmocked.get, {
apply(target, that, args) {
//Arguments
const [url, options] = args
//Search for mocked request
for (const service of Object.keys(mocks.axios.get)) {
const mocked = mocks.axios.get[service]({faker, url, options})
if (mocked)
return mocked
}
//Unmocked call
return target(...args)
},
})
}
//RSS mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking rss-parser")
//Mock rss feed
rss.prototype.parseURL = function(url) {
console.debug(`metrics/compute/mocks > mocking rss feed result > ${url}`)
return ({
items: new Array(30).fill(null).map(_ => ({
title: faker.lorem.sentence(),
link: faker.internet.url(),
content: faker.lorem.paragraphs(),
contentSnippet: faker.lorem.paragraph(),
isoDate: faker.date.recent(),
})),
title: faker.lorem.words(),
description: faker.lorem.paragraph(),
link: url,
})
}
}
//Google API mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking google-maps-services-js")
//Mock geocode API
Gmap.prototype.geocode = function() {
console.debug("metrics/compute/mocks > mocking google maps geocode result")
const lat = faker.location.latitude()
const lng = faker.location.longitude()
const city = faker.location.city()
const country = faker.location.country()
return {
data: {
results: [
{
address_components: [
{
long_name: city,
short_name: city,
types: ["political"],
},
{
long_name: country,
short_name: faker.location.countryCode(),
types: ["country", "political"],
},
],
formatted_address: `${city}, ${country}`,
geometry: {
bounds: {
northeast: {lat, lng},
southwest: {lat, lng},
},
location: {lat, lng},
location_type: "APPROXIMATE",
viewport: {
northeast: {lat, lng},
southwest: {lat, lng},
},
},
place_id: "ChIJu9FC7RXupzsR26dsAapFLgg",
types: ["locality", "political"],
},
],
},
}
}
}
//Return mocked elements
return {graphql, rest}
}