Skip to content

Commit 159ead8

Browse files
committed
Fix more node-fetch related-issues
1 parent aa0757a commit 159ead8

File tree

3 files changed

+64
-62
lines changed

3 files changed

+64
-62
lines changed

plugins/docusaurus-plugin-ionic-component-api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fetch = globalThis.fetch;
2+
13
module.exports = function (context, options) {
24
return {
35
name: 'docusaurus-plugin-ionic-component-api',

scripts/api-ja.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const fetch = require('node-fetch');
1+
const fetch = globalThis.fetch;
2+
23
const fs = require('fs');
34
const path = require('path');
45
const { api: apiOverrides } = require('./data/meta-override.json');
Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
1-
import dotenv from 'dotenv';
2-
import { outputJson } from 'fs-extra';
3-
import fetch from 'node-fetch';
4-
import { resolve } from 'path';
5-
import url from 'url';
6-
7-
dotenv.config();
8-
9-
const OUTPUT_PATH = resolve(
10-
__dirname,
11-
'../data/github-commits.json'
12-
);
13-
14-
export default {
15-
title: 'Getting a list of past commits',
16-
task: async (_: any, task: any) => {
17-
const History = await getAllGHCommits(task);
18-
if (Object.keys(History).length > 0) {
19-
outputJson(OUTPUT_PATH, History, { spaces: 2 });
20-
}
21-
}
22-
};
23-
24-
const getAllGHCommits = async (task: any, page = 1) => {
25-
try {
26-
task.output = `Getting commits: ${page * 100 - 99} - ${page * 100}`;
27-
const request = await fetch(
28-
url.format({
29-
protocol: 'https',
30-
hostname: 'api.github.com',
31-
pathname: 'repos/ionic-team/ionic-docs/commits',
32-
query: {
33-
per_page: 100,
34-
page
35-
}
36-
}), {
37-
headers: {
38-
'Authorization': process.env.GITHUB_TOKEN !== undefined ? `token ${process.env.GITHUB_TOKEN}` : ''
39-
}
40-
}
41-
);
42-
43-
let commits = await request.json().then(list => list.reduce((obj: any, commit: any) => {
44-
obj[commit.sha] = {
45-
id: commit.author.login,
46-
avatar: commit.author.avatar_url,
47-
time: commit.commit.committer.date
48-
};
49-
return obj;
50-
}, {}));
51-
52-
// recursively get more commits if there are more to get, limit 2k
53-
if (Object.keys(commits).length === 100 && page < 20) {
54-
commits = { ...commits, ...await getAllGHCommits(task, page + 1) };
55-
}
56-
57-
return commits;
58-
} catch {
59-
return {};
60-
}
61-
};
1+
import dotenv from 'dotenv';
2+
import { outputJson } from 'fs-extra';
3+
import { resolve } from 'path';
4+
import url from 'url';
5+
6+
dotenv.config();
7+
8+
const OUTPUT_PATH = resolve(
9+
__dirname,
10+
'../data/github-commits.json'
11+
);
12+
13+
export default {
14+
title: 'Getting a list of past commits',
15+
task: async (_: any, task: any) => {
16+
const History = await getAllGHCommits(task);
17+
if (Object.keys(History).length > 0) {
18+
outputJson(OUTPUT_PATH, History, { spaces: 2 });
19+
}
20+
}
21+
};
22+
23+
const getAllGHCommits = async (task: any, page = 1) => {
24+
try {
25+
task.output = `Getting commits: ${page * 100 - 99} - ${page * 100}`;
26+
const request = await fetch(
27+
url.format({
28+
protocol: 'https',
29+
hostname: 'api.github.com',
30+
pathname: 'repos/ionic-team/ionic-docs/commits',
31+
query: {
32+
per_page: 100,
33+
page
34+
}
35+
}), {
36+
headers: {
37+
'Authorization': process.env.GITHUB_TOKEN !== undefined ? `token ${process.env.GITHUB_TOKEN}` : ''
38+
}
39+
}
40+
);
41+
42+
let commits = await request.json().then(list => list.reduce((obj: any, commit: any) => {
43+
obj[commit.sha] = {
44+
id: commit.author.login,
45+
avatar: commit.author.avatar_url,
46+
time: commit.commit.committer.date
47+
};
48+
return obj;
49+
}, {}));
50+
51+
// recursively get more commits if there are more to get, limit 2k
52+
if (Object.keys(commits).length === 100 && page < 20) {
53+
commits = { ...commits, ...await getAllGHCommits(task, page + 1) };
54+
}
55+
56+
return commits;
57+
} catch {
58+
return {};
59+
}
60+
};

0 commit comments

Comments
 (0)