forked from directus/gatsby-source-directus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
353 lines (288 loc) · 8.71 KB
/
gatsby-node.js
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
// @ts-check
const ms = require('ms');
const chalk = require('chalk');
const { Directus } = require('@directus/sdk');
const { sourceNodes, createSchemaCustomization } = require('gatsby-source-graphql/gatsby-node');
const { createRemoteFileNode } = require('gatsby-source-filesystem');
const nodeFetch = require('node-fetch').default;
/**
* Validate plugin options
*/
exports.pluginOptionsSchema = ({ Joi }) => {
return Joi.object().keys({
url: Joi.string().required(),
auth: Joi.object()
.keys({
token: Joi.string(),
email: Joi.string(),
password: Joi.string(),
})
.with('email', 'password')
.with('password', 'email')
.xor('token', 'email'),
type: Joi.object()
.keys({
name: Joi.string(),
field: Joi.string(),
})
.optional(),
dev: Joi.object().keys({
refresh: [Joi.number(), Joi.string()],
}),
graphql: Joi.object(),
concurrency: Joi.number().default(10),
retries: Joi.number().default(5),
});
};
/**
* Gatsby source implementation.
*/
exports.sourceNodes = async (gatsbyOptions, pluginOptions) => {
const {
actions: { createNode, touchNode },
createNodeId,
store,
cache,
reporter,
getNode,
} = gatsbyOptions;
const { headers } = await plugin.getOptions();
const { Authorization } = await headers();
await plugin.setOptions(pluginOptions);
const optionsSystem = plugin.getOptionsSystem();
const options = plugin.getOptions();
// Avoid type conflict with gatsby-source-graphql
gatsbyOptions.actions.createNode = (node) => {
if (node.internal.type === 'GraphQLSource') {
if (node.typeName === optionsSystem.typeName) node.internal.type = 'DirectusSystemGraphQLSource';
if (node.typeName === options.typeName) node.internal.type = 'DirectusGraphQLSource';
}
return createNode(node);
};
await sourceNodes(gatsbyOptions, optionsSystem);
await sourceNodes(gatsbyOptions, options);
// Load files here rather than on file resolution.
// Create a node for each file and store it in the cache
// so it can bre retrieved on file resolution.
for await (const files of plugin.iterateFiles()) {
if (!files?.length) break;
await Promise.all(
files.map(async (file) => {
const cached = await cache.get(file.id);
const node = cached && getNode(cached.nodeId);
if (node) {
touchNode(node);
return;
}
const nameParts = file.filename_download.split('.');
const ext = nameParts.length > 1 ? `.${nameParts.pop()}` : '';
const name = nameParts.join('.');
const fileUrl = `${plugin.url}assets/${file.id}`;
const fileNode = await createRemoteFileNode({
url: fileUrl,
parentNodeId: file.id,
store,
cache,
createNode,
createNodeId,
httpHeaders: { Authorization },
reporter,
ext,
name,
});
await cache.set(file.id, { nodeId: fileNode.id });
})
);
}
};
exports.createSchemaCustomization = async (gatsby, pluginOptions) => {
await plugin.setOptions(pluginOptions);
await createSchemaCustomization(gatsby, plugin.getOptionsSystem());
await createSchemaCustomization(gatsby, plugin.getOptions());
};
/**
* Gatsby file implementation.
*/
exports.createResolvers = async ({ cache, createResolvers, getNode }, pluginOptions) => {
await plugin.setOptions(pluginOptions);
const fileResolver = {
imageFile: {
type: `File`,
async resolve(source) {
const cached = await cache.get(source.id);
if (cached) return getNode(cached.nodeId);
throw new Error('Cached image not found for id: ' + source.id);
},
},
};
await createResolvers({
DirectusData_directus_files: fileResolver,
DirectusSystemData_directus_files: fileResolver,
});
};
class Plugin {
constructor() {
// eslint-disable-next-line no-undef
this.fileCache = new Map();
this.directus = null;
this.options = null;
this.urlGraphqlSystem = '';
this.urlGraphql = '';
this.url = '';
this.refreshInterval = 0;
this.authPromise = null;
this.concurrency = 10;
this.retries = 5;
}
async setOptions(options) {
const { url, dev, auth, concurrency, retries } = options;
if (isEmpty(url)) error('"url" must be defined');
if (this.directus) return this.authPromise;
const hasAuth = !!auth;
const hasToken = !isEmpty(auth?.token);
const hasEmail = !isEmpty(auth?.email);
const hasPassword = !isEmpty(auth?.password);
const hasCredentials = hasEmail && hasPassword;
if (hasAuth) {
if (!hasToken && !hasCredentials) error('"auth.token" or ("auth.email" and "auth.password") must be defined');
} else warning('no "auth" option were defined. Resources will be fetched with public role');
try {
const baseUrl = new URL(url);
const basePath = baseUrl.pathname.endsWith('/') ? baseUrl.pathname.slice(0, -1) : baseUrl.pathname;
baseUrl.pathname = basePath;
this.url = baseUrl.toString();
baseUrl.pathname = basePath + '/graphql';
this.urlGraphql = baseUrl.toString();
baseUrl.pathname = basePath + '/graphql/system';
this.urlGraphqlSystem = baseUrl.toString();
} catch (err) {
error('"url" should be a valid URL');
}
try {
this.directus = new Directus(this.url);
if (hasToken) this.authPromise = await this.directus.auth.static(auth.token);
if (hasCredentials)
this.authPromise = await this.directus.auth.login({ email: auth?.email, password: auth?.password });
} catch (err) {
error(`authentication failed with: ${err.message}\nAre credentials valid?`);
}
this.refreshInterval = typeof dev?.refresh === 'string' ? ms(dev.refresh) / 1000 : dev?.refresh || 15;
if (Number.isNaN(this.refreshInterval))
error('"dev.refresh" should be a number in seconds or a string with ms format, i.e. 5s, 5m, 5h, ...');
this.options = options;
this.concurrency = concurrency;
this.retries = retries;
return this.authPromise;
}
getOptions() {
const internalOptions = ['url', 'dev', 'auth', 'type'];
const gatsbyPluginOptions = Object.fromEntries(
Object.entries(this.options).flatMap(([key, value]) => (internalOptions.includes(key) ? [] : [[key, value]]))
);
return {
...this.options.graphql,
...gatsbyPluginOptions,
url: this.urlGraphql,
typeName: this.options?.type?.name || 'DirectusData',
fieldName: this.options?.type?.field || 'directus',
headers: this.headers.bind(this),
fetch: async (uri, options) => {
function request() {
return nodeFetch(uri, options);
}
let count = 0;
let response = null;
let error = null;
while (response === null && count++ < this.retries) {
try {
response = await request();
} catch (err) {
error = err;
}
if (count > 0) {
await sleep(Math.pow(2, count) * 1000);
}
}
if (response === null) throw error;
return response;
},
};
}
getOptionsSystem() {
const options = this.getOptions();
return {
...options,
url: this.urlGraphqlSystem,
typeName: this.options?.type?.system_name || 'DirectusSystemData',
fieldName: this.options?.type?.system_field || 'directus_system',
};
}
async *iterateFiles() {
if (!this.directus) throw new Error('Directus is not instantiated');
let hasMore = true;
let page = 1;
while (hasMore) {
if (!this.directus) throw new Error('Directus is not instantiated');
const files = await this.directus.files
.readByQuery({
fields: ['id', 'type', 'filename_download'],
sort: ['id'],
limit: this.concurrency,
page,
})
.then((r) => r?.data ?? []);
yield files;
if (files.length < this.concurrency) {
hasMore = false;
} else {
page++;
}
}
}
async headers() {
if (!this.directus) throw new Error('Directus is not instantiated');
let headers = {};
if (typeof this.options?.headers === 'object') {
Object.assign(headers, this.options.headers || {});
} else if (typeof this.options?.headers === 'function') {
Object.assign(headers, (await this.options.headers()) || {});
}
const token = await this.directus.auth.token;
if (token) {
Object.assign(headers, {
Authorization: `Bearer ${token}`,
});
}
return headers;
}
}
class Log {
static log(level, message) {
let color = level === 'error' ? 'red' : level === 'warning' ? 'yellow' : 'white';
// eslint-disable-next-line no-console
console.log(chalk.cyan('gatsby-source-directus'), ':', chalk[color](message));
}
static error(message) {
Log.log('error', message);
}
static warning(message) {
Log.log('error', message);
}
}
function isEmpty(value) {
if (value?.constructor === String) return value.length === 0;
return true;
}
function error(message) {
Log.error(message);
const error = new Error(`gatsby-source-directus: ${message}`);
error.stack = undefined;
throw error;
}
function warning(message) {
Log.warning(message);
}
function sleep(ms) {
return new Promise((res) => setTimeout(res, ms));
}
const plugin = new Plugin();