From 4686ca291099e5d50d7897f0fad153a2eb3cb376 Mon Sep 17 00:00:00 2001 From: Robert Halter Date: Wed, 20 Sep 2023 10:07:20 +0000 Subject: [PATCH 1/5] compile with declare --- consts.d.ts | 2 ++ decs.test-d.d.ts | 1 + githubApiGet.d.ts | 17 ++++++--- githubApiGet.js | 92 +++++++++++++++++++++++------------------------ package-lock.json | 4 +-- package.json | 2 +- test.d.ts | 1 + test.js | 54 ++++++++++++++-------------- tsconfig.json | 3 +- 9 files changed, 95 insertions(+), 81 deletions(-) create mode 100644 consts.d.ts create mode 100644 decs.test-d.d.ts create mode 100644 test.d.ts diff --git a/consts.d.ts b/consts.d.ts new file mode 100644 index 0000000..06af829 --- /dev/null +++ b/consts.d.ts @@ -0,0 +1,2 @@ +export declare const GITHUB_API_BASE_URL = "https://api.github.com/"; +export declare const GITHUB_HTML_BASE_URL = "https://github.com/"; diff --git a/decs.test-d.d.ts b/decs.test-d.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/decs.test-d.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/githubApiGet.d.ts b/githubApiGet.d.ts index bd24c1b..e8f34ce 100644 --- a/githubApiGet.d.ts +++ b/githubApiGet.d.ts @@ -1,4 +1,13 @@ -// declare here a module with name github-api-get -declare module "github-api-get"; - -// TODO declare function type +/** + * Returns the github topics of a project. + * + * @remarks + * This method is part of the {@link githubApi | github-api}. + * + * @param {String} loginName - The github login name + * @param {String} projectName - The github project name + * @param {boolean} infoLog - true Logs group and info into the console + * @param {boolean} isProd - true Calls fetch to github api, false returns mock data with same structure + * @returns {Promise} The github topics of a project + */ +export declare function getGithubTopics(loginName: String, projectName: String, infoLog: boolean, isProd: boolean): Promise; diff --git a/githubApiGet.js b/githubApiGet.js index 613b6bb..9c4bdb7 100644 --- a/githubApiGet.js +++ b/githubApiGet.js @@ -16,25 +16,26 @@ import { GITHUB_API_BASE_URL } from "./consts.js"; * @returns {Promise} The github topics of a project */ export async function getGithubTopics(loginName, projectName, infoLog, isProd) { - if (infoLog) { - console.group("getGithubTopics"); - } - if (loginName && projectName) { if (infoLog) { - console.info("githubApi.ts: loginName: " + loginName); - console.info("githubApi.ts: projectName: " + projectName); + console.group("getGithubTopics"); } - if (infoLog) { - console.groupEnd(); + if (loginName && projectName) { + if (infoLog) { + console.info("githubApi.ts: loginName: " + loginName); + console.info("githubApi.ts: projectName: " + projectName); + } + if (infoLog) { + console.groupEnd(); + } + // https://www.npmjs.com/package/jsonpath MIT License + return Promise.resolve(jp.query(await returnData(loginName, projectName, infoLog, isProd), "$.topics[*]")); } - // https://www.npmjs.com/package/jsonpath MIT License - return Promise.resolve(jp.query(await returnData(loginName, projectName, infoLog, isProd), "$.topics[*]")); - } else { - if (infoLog) { - console.groupEnd(); + else { + if (infoLog) { + console.groupEnd(); + } + return Promise.resolve([]); } - return Promise.resolve([]); - } } /** * Returns the github the project object / json of a project. @@ -49,36 +50,35 @@ export async function getGithubTopics(loginName, projectName, infoLog, isProd) { * @returns {Promise} The github the project object / json of a project */ async function returnData(loginName, projectName, infoLog, isProd) { - if (isProd) { - // get from github project api - const GITHUB_API_PROJECT_URL = GITHUB_API_BASE_URL + "repos/" + loginName + "/"; - const response = await fetch(GITHUB_API_PROJECT_URL + projectName); - if (!response.ok) { - if (infoLog) { - console.info( - "API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", - ); - console.info("https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"); - } - // Github has a Rate Limit - // in this case Status is 403 Forbidden - // and return this Message Object / Json: - // { - // message: "API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", - // documentation_url: 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting' - // } + if (isProd) { + // get from github project api + const GITHUB_API_PROJECT_URL = GITHUB_API_BASE_URL + "repos/" + loginName + "/"; + const response = await fetch(GITHUB_API_PROJECT_URL + projectName); + if (!response.ok) { + if (infoLog) { + console.info("API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)"); + console.info("https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"); + } + // Github has a Rate Limit + // in this case Status is 403 Forbidden + // and return this Message Object / Json: + // { + // message: "API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", + // documentation_url: 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting' + // } + } + return await response.json(); + } + else { + // return mock data + // based on + // github-api/01-01-vanilla-HTML5-starter-page.json + return Promise.resolve({ + name: "01-01-vanilla-HTML5-starter-page", + html_url: "https://github.com/roebi/01-01-vanilla-HTML5-starter-page", + description: "vanilla HTML 5 starter page - Have you ever heard of this HTML 5 tags ?", + homepage: "https://roebi.github.io/01-01-vanilla-HTML5-starter-page/", + topics: [" mockdata!", "html5", "html5-template", "roebi", "starter"], + }); } - return await response.json(); - } else { - // return mock data - // based on - // github-api/01-01-vanilla-HTML5-starter-page.json - return Promise.resolve({ - name: "01-01-vanilla-HTML5-starter-page", - html_url: "https://github.com/roebi/01-01-vanilla-HTML5-starter-page", - description: "vanilla HTML 5 starter page - Have you ever heard of this HTML 5 tags ?", - homepage: "https://roebi.github.io/01-01-vanilla-HTML5-starter-page/", - topics: [" mockdata!", "html5", "html5-template", "roebi", "starter"], - }); - } } diff --git a/package-lock.json b/package-lock.json index 47c151c..b26936c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "github-api-get", - "version": "0.3.6", + "version": "0.3.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "github-api-get", - "version": "0.3.6", + "version": "0.3.7", "license": "MIT", "dependencies": { "jsonpath": "^1.1.1", diff --git a/package.json b/package.json index 20fa0ad..8cb2f0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "github-api-get", - "version": "0.3.6", + "version": "0.3.7", "description": "use github api", "type": "module", "exports": { diff --git a/test.d.ts b/test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/test.js b/test.js index 4bd6a0d..7f84cc6 100644 --- a/test.js +++ b/test.js @@ -1,41 +1,41 @@ import test from "ava"; import { getGithubTopics } from "./githubApiGet.js"; test("foo", (t) => { - t.pass(); + t.pass(); }); test("bar", async (t) => { - const bar = Promise.resolve("bar"); - t.is(await bar, "bar"); + const bar = Promise.resolve("bar"); + t.is(await bar, "bar"); }); test("mockdataTopics", async (t) => { - const loginName = "roebi"; - const projectName = "01-01-vanilla-HTML5-starter-page"; - const infoLog = true; - const isProd = false; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); - t.deepEqual(await githubTopics, [" mockdata!", "html5", "html5-template", "roebi", "starter"]); + const loginName = "roebi"; + const projectName = "01-01-vanilla-HTML5-starter-page"; + const infoLog = true; + const isProd = false; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); + t.deepEqual(await githubTopics, [" mockdata!", "html5", "html5-template", "roebi", "starter"]); }); test("noTopics", async (t) => { - const loginName = ""; - const projectName = ""; - const infoLog = true; - const isProd = false; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); - t.deepEqual(await githubTopics, []); + const loginName = ""; + const projectName = ""; + const infoLog = true; + const isProd = false; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); + t.deepEqual(await githubTopics, []); }); test("prodDataTopics", async (t) => { - const loginName = "roebi"; - const projectName = "01-01-vanilla-HTML5-starter-page"; - const infoLog = true; - const isProd = true; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); - t.deepEqual(await githubTopics, ["css3", "html5", "html5-template", "roebi", "starter"]); + const loginName = "roebi"; + const projectName = "01-01-vanilla-HTML5-starter-page"; + const infoLog = true; + const isProd = true; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); + t.deepEqual(await githubTopics, ["css3", "html5", "html5-template", "roebi", "starter"]); }); test("prodDataNoTopicsInvalidProjectName", async (t) => { - const loginName = "roebi"; - const projectName = "project-not-exist"; - const infoLog = true; - const isProd = true; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); - t.deepEqual(await githubTopics, []); + const loginName = "roebi"; + const projectName = "project-not-exist"; + const infoLog = true; + const isProd = true; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd); + t.deepEqual(await githubTopics, []); }); diff --git a/tsconfig.json b/tsconfig.json index bfa8006..f8a7364 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "@tsconfig/node18/tsconfig.json", "compilerOptions": { - "strictNullChecks": true + "strictNullChecks": true, + "declaration": true } } From f6aa1a320438ec4efae5fecdb56e35c9ca0e9d7c Mon Sep 17 00:00:00 2001 From: Robert Halter Date: Wed, 20 Sep 2023 10:11:24 +0000 Subject: [PATCH 2/5] test and prettier --- coverage/consts.js.html | 2 +- coverage/githubApiGet.js.html | 2 +- coverage/index.html | 2 +- ...son => coverage-5563-1695204567034-1.json} | 990 +++++++++--------- ...son => coverage-5563-1695204567090-0.json} | 639 +++++------ githubApiGet.d.ts | 7 +- githubApiGet.js | 92 +- test.js | 54 +- 8 files changed, 897 insertions(+), 891 deletions(-) rename coverage/tmp/{coverage-13023-1695166635381-1.json => coverage-5563-1695204567034-1.json} (99%) rename coverage/tmp/{coverage-13023-1695166635454-0.json => coverage-5563-1695204567090-0.json} (99%) diff --git a/coverage/consts.js.html b/coverage/consts.js.html index dd1b058..468b857 100644 --- a/coverage/consts.js.html +++ b/coverage/consts.js.html @@ -86,7 +86,7 @@

All files consts.js

+ + + + + + + +
+

Source: githubApiGet.js

+ +
+
+
// githubApi.ts
+import fetch from "node-fetch";
+import jp from "jsonpath";
+import { GITHUB_API_BASE_URL } from "./consts.js";
+// const isProd = false; // import.meta.env.PROD; // false;
+/**
+ * Returns the github topics of a project.
+ *
+ * @remarks
+ * This method is part of the {@link githubApi | github-api}.
+ *
+ * @param {string} loginName - The github login name
+ * @param {string} projectName - The github project name
+ * @param {boolean} infoLog - true Logs group and info into the console
+ * @param {boolean} isProd - true Calls fetch to github api, false returns mock data with same structure
+ * @returns {Promise<string[]>} The github topics of a project
+ */
+export async function getGithubTopics(loginName, projectName, infoLog, isProd) {
+    if (infoLog) {
+        console.group("getGithubTopics");
+    }
+    if (loginName && projectName) {
+        if (infoLog) {
+            console.info("githubApi.ts: loginName: " + loginName);
+            console.info("githubApi.ts: projectName: " + projectName);
+        }
+        if (infoLog) {
+            console.groupEnd();
+        }
+        // https://www.npmjs.com/package/jsonpath MIT License
+        return Promise.resolve(jp.query(await returnData(loginName, projectName, infoLog, isProd), "$.topics[*]"));
+    }
+    else {
+        if (infoLog) {
+            console.groupEnd();
+        }
+        return Promise.resolve([]);
+    }
+}
+/**
+ * Returns the github the project object / json of a project.
+ *
+ * @remarks
+ * This method is part of the {@link githubApi | github-api}.
+ *
+ * @param {string} loginName - The github login name
+ * @param {string} projectName - The github project name
+ * @param {boolean} infoLog - true Logs group and info into the console
+ * @param {boolean} isProd - true Calls fetch to github api, false returns mock data with same structure
+ * @returns {Promise<any>} The github the project object / json of a project
+ */
+async function returnData(loginName, projectName, infoLog, isProd) {
+    if (isProd) {
+        // get from github project api
+        const GITHUB_API_PROJECT_URL = GITHUB_API_BASE_URL + "repos/" + loginName + "/";
+        const response = await fetch(GITHUB_API_PROJECT_URL + projectName);
+        if (!response.ok) {
+            if (infoLog) {
+                console.info("API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)");
+                console.info("https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting");
+            }
+            // Github has a Rate Limit
+            // in this case Status is 403 Forbidden
+            // and return this Message Object / Json:
+            // {
+            //   message: "API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
+            //   documentation_url: 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting'
+            // }
+        }
+        return await response.json();
+    }
+    else {
+        // return mock data
+        // based on
+        // github-api/01-01-vanilla-HTML5-starter-page.json
+        return Promise.resolve({
+            name: "01-01-vanilla-HTML5-starter-page",
+            html_url: "https://github.com/roebi/01-01-vanilla-HTML5-starter-page",
+            description: "vanilla HTML 5 starter page - Have you ever heard of this HTML 5 tags ?",
+            homepage: "https://roebi.github.io/01-01-vanilla-HTML5-starter-page/",
+            topics: [" mockdata!", "html5", "html5-template", "roebi", "starter"],
+        });
+    }
+}
+
+
+
+
+ + + +
+ +
+ Documentation generated by JSDoc 4.0.2 on Wed Sep 20 2023 10:19:10 + GMT+0000 (Coordinated Universal Time) +
+ + + + + diff --git a/jsdoc/global.html b/jsdoc/global.html index 500cade..2faae1c 100644 --- a/jsdoc/global.html +++ b/jsdoc/global.html @@ -32,7 +32,7 @@

Methods

getGithubTopics(loginName, projectName, infoLog, isProd) → {Promise.<Array.<String>>} + > → {Promise.<Array.<string>>}

@@ -57,7 +57,7 @@
Parameters:
loginName - String + string

The github login name

@@ -67,7 +67,7 @@
Parameters:
projectName - String + string

The github project name

@@ -101,7 +101,9 @@
Parameters:
Source:
@@ -115,7 +117,7 @@
Returns:
Type
- Promise.<Array.<String>> + Promise.<Array.<string>>
@@ -147,7 +149,7 @@
Parameters:
loginName - String + string

The github login name

@@ -157,7 +159,7 @@
Parameters:
projectName - String + string

The github project name

@@ -191,7 +193,9 @@
Parameters:
Source:
@@ -224,7 +228,7 @@

Global


- Documentation generated by JSDoc 4.0.2 on Fri Sep 15 2023 19:23:27 + Documentation generated by JSDoc 4.0.2 on Wed Sep 20 2023 10:19:10 GMT+0000 (Coordinated Universal Time)
diff --git a/jsdoc/index.html b/jsdoc/index.html index 01eb0d2..bfd242e 100644 --- a/jsdoc/index.html +++ b/jsdoc/index.html @@ -32,7 +32,7 @@

Global


- Documentation generated by JSDoc 4.0.2 on Fri Sep 15 2023 19:23:27 + Documentation generated by JSDoc 4.0.2 on Wed Sep 20 2023 10:19:10 GMT+0000 (Coordinated Universal Time)
diff --git a/test.ts b/test.ts index e7caec9..1099563 100644 --- a/test.ts +++ b/test.ts @@ -15,7 +15,7 @@ test("mockdataTopics", async (t) => { const projectName = "01-01-vanilla-HTML5-starter-page"; const infoLog = true; const isProd = false; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; t.deepEqual(await githubTopics, [" mockdata!", "html5", "html5-template", "roebi", "starter"]); }); @@ -24,7 +24,7 @@ test("noTopics", async (t) => { const projectName = ""; const infoLog = true; const isProd = false; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; t.deepEqual(await githubTopics, []); }); From 8d118b33b422bbc93a3d5ad271fe01081f553d6d Mon Sep 17 00:00:00 2001 From: Robert Halter Date: Wed, 20 Sep 2023 10:25:33 +0000 Subject: [PATCH 4/5] String -> string --- coverage/consts.js.html | 2 +- coverage/githubApiGet.js.html | 2 +- coverage/index.html | 2 +- ...on => coverage-12295-1695205410699-1.json} | 6 +- ...on => coverage-12295-1695205410768-0.json} | 68 +++++++++---------- test.ts | 4 +- 6 files changed, 42 insertions(+), 42 deletions(-) rename coverage/tmp/{coverage-10223-1695205172519-1.json => coverage-12295-1695205410699-1.json} (99%) rename coverage/tmp/{coverage-10223-1695205172574-0.json => coverage-12295-1695205410768-0.json} (99%) diff --git a/coverage/consts.js.html b/coverage/consts.js.html index 768f38a..d899b08 100644 --- a/coverage/consts.js.html +++ b/coverage/consts.js.html @@ -86,7 +86,7 @@

All files consts.js