From 16aae73412b24f0526634209b0febda776e6ed3a Mon Sep 17 00:00:00 2001 From: Daan Klarenbeek Date: Tue, 20 Jun 2023 16:12:56 +0200 Subject: [PATCH] feat: add @ijsblokje/utils --- packages/utils/package.json | 31 ++++++++++++++++++ packages/utils/src/RequestWithPagination.ts | 26 +++++++++++++++ packages/utils/src/constants.ts | 2 ++ packages/utils/src/types.ts | 35 +++++++++++++++++++++ packages/utils/tsconfig.eslint.json | 4 +++ packages/utils/tsconfig.json | 5 +++ 6 files changed, 103 insertions(+) create mode 100644 packages/utils/package.json create mode 100644 packages/utils/src/RequestWithPagination.ts create mode 100644 packages/utils/src/constants.ts create mode 100644 packages/utils/src/types.ts create mode 100644 packages/utils/tsconfig.eslint.json create mode 100644 packages/utils/tsconfig.json diff --git a/packages/utils/package.json b/packages/utils/package.json new file mode 100644 index 00000000..6c506f28 --- /dev/null +++ b/packages/utils/package.json @@ -0,0 +1,31 @@ +{ + "name": "@ijsblokje/utils", + "version": "0.0.0", + "author": "ijsKoud ", + "license": "MIT", + "type": "module", + "exports": { + "./types.js": "./dist/types.js", + "./constants.js": "./dist/constants.js", + "./RequestWithPagination.js": "./dist/RequestWithPagination.js" + }, + "scripts": { + "build": "tsc --build", + "build:watch": "tsc --watch > /dev/null", + "lint": "TIMING=1 eslint" + }, + "dependencies": { + "lru-cache": "^10.0.0", + "probot": "12.3.1" + }, + "devDependencies": { + "@types/node": "^18.16.18", + "eslint": "^8.43.0", + "prettier": "^2.8.8", + "typescript": "5.1.3" + }, + "engines": { + "node": ">= v18.16.0" + }, + "packageManager": "yarn@3.6.0" +} diff --git a/packages/utils/src/RequestWithPagination.ts b/packages/utils/src/RequestWithPagination.ts new file mode 100644 index 00000000..219508e5 --- /dev/null +++ b/packages/utils/src/RequestWithPagination.ts @@ -0,0 +1,26 @@ +/** + * Handle the pagination of an Octokit request + * @param request The function to call everytime we need to fetch more data + * @param check The function to check if we reached the desired amount or not + * @returns + */ +async function requestWithPagination(request: RequestWithPaginationRequest, check: RequestWithPaginationCheck): Promise { + const data: R[] = []; + let page = 1; + + let response = await request(page); + data.push(response); + + while (check(response)) { + response = await request(page); + data.push(response); + page++; + } + + return data; +} + +export type RequestWithPaginationRequest = (page: number) => Promise; +export type RequestWithPaginationCheck = (response: R) => boolean; + +export default requestWithPagination; diff --git a/packages/utils/src/constants.ts b/packages/utils/src/constants.ts new file mode 100644 index 00000000..0c575acc --- /dev/null +++ b/packages/utils/src/constants.ts @@ -0,0 +1,2 @@ +export const README_TEMPLATE_LOCATION = "config/readme.md"; +export const LABEL_CONFIG_LOCATION = "config/labels.json"; diff --git a/packages/utils/src/types.ts b/packages/utils/src/types.ts new file mode 100644 index 00000000..9f861aaf --- /dev/null +++ b/packages/utils/src/types.ts @@ -0,0 +1,35 @@ +export interface GitHubRepoContext { + /** The owner of the repository */ + owner: string; + + /** The repository name */ + repo: string; +} + +export interface Label { + /** The name of the label */ + name: string; + + /** The label description */ + description: string; + + /** The label color in hex format */ + color: string; +} + +export interface OctokitAuthOptions { + /** The id of the GitHub application */ + appId: number; + + /** The GitHub private key for signing data */ + privateKey: string; + + /** The GitHub client id */ + clientId: string; + + /** The GitHub client secret */ + clientSecret: string; + + /** The installation id */ + installationId?: number; +} diff --git a/packages/utils/tsconfig.eslint.json b/packages/utils/tsconfig.eslint.json new file mode 100644 index 00000000..500e38bb --- /dev/null +++ b/packages/utils/tsconfig.eslint.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "include": ["./src/**/*"] +} diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json new file mode 100644 index 00000000..d4f055b3 --- /dev/null +++ b/packages/utils/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../../tsconfig.json", + "include": ["./src/**/*.ts"], + "compilerOptions": { "outDir": "./dist" } +}