diff --git a/README.md b/README.md index b60f23e..4d0eef3 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ export async function getGithubTopicsFromGithub() { const infoLog = true; const isProd = true; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; const realGithubTopics = await githubTopics; console.group("realGithubTopics"); 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/coverage/consts.js.html b/coverage/consts.js.html index dd1b058..d899b08 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/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.ts b/test.ts index e7caec9..00e3265 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, []); }); @@ -33,7 +33,7 @@ test("prodDataTopics", async (t) => { const projectName = "01-01-vanilla-HTML5-starter-page"; const infoLog = true; const isProd = true; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; t.deepEqual(await githubTopics, ["css3", "html5", "html5-template", "roebi", "starter"]); }); @@ -42,6 +42,6 @@ test("prodDataNoTopicsInvalidProjectName", async (t) => { const projectName = "project-not-exist"; const infoLog = true; const isProd = true; - const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; + const githubTopics = getGithubTopics(loginName, projectName, infoLog, isProd) as Promise; 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 } }