-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpullFragmenttypes.js
executable file
·78 lines (70 loc) · 2.01 KB
/
pullFragmenttypes.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
#!/usr/bin/env node
import dotenv from 'dotenv';
import {writeFileSync} from 'fs';
import https from 'https';
dotenv.config();
const repoId = process.env.VITE_PRISMIC_REPO;
const fetchJson = (url, opts = {}) => {
return new Promise((resolve, reject) => {
https.get(url, opts, (res) => {
const {statusCode} = res;
const contentType = res.headers['content-type'];
let error;
// Any 2xx status code signals a successful response but
// here we're only checking for 200.
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.error(error.message);
// Consume response data to free up memory
res.resume();
reject(error);
return;
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => {
rawData += chunk;
});
res.on("close", () => {
try {
const parsedData = JSON.parse(rawData);
resolve(parsedData);
} catch (e) {
reject(e);
}
});
}).on("error", (e) => {
reject(e);
});
});
}
fetchJson(`https://${repoId}.cdn.prismic.io/api/v2`).then((data) => {
const ref = data.refs.find((r) => r.id === 'master');
if (!ref) return;
fetchJson(
`https://${repoId}.cdn.prismic.io/graphql?query=%7B%20__schema%20%7B%20types%20%7B%20kind%20name%20possibleTypes%20%7B%20name%20%7D%20%7D%20%7D%20%7D`,
{
headers: {
'prismic-ref': ref.ref,
},
},
).then((result) => {
const filteredResults = result;
filteredResults.data.__schema.types = result.data.__schema.types.filter(
(type) => type.possibleTypes !== null,
);
writeFileSync('./src/lib/graphql/fragmentTypes.json', JSON.stringify(filteredResults.data), (err) => {
if (err) {
console.error('Error writing fragmentTypes file', err);
} else {
console.log('Fragment types successfully extracted!');
}
});
});
});