Skip to content

Commit

Permalink
feat: add @ijsblokje/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Jun 20, 2023
1 parent 8b437de commit 16aae73
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@ijsblokje/utils",
"version": "0.0.0",
"author": "ijsKoud <[email protected]>",
"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": "[email protected]"
}
26 changes: 26 additions & 0 deletions packages/utils/src/RequestWithPagination.ts
Original file line number Diff line number Diff line change
@@ -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<R>(request: RequestWithPaginationRequest<R>, check: RequestWithPaginationCheck<R>): Promise<R[]> {
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<R> = (page: number) => Promise<R>;
export type RequestWithPaginationCheck<R> = (response: R) => boolean;

export default requestWithPagination;
2 changes: 2 additions & 0 deletions packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const README_TEMPLATE_LOCATION = "config/readme.md";
export const LABEL_CONFIG_LOCATION = "config/labels.json";
35 changes: 35 additions & 0 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions packages/utils/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["./src/**/*"]
}
5 changes: 5 additions & 0 deletions packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["./src/**/*.ts"],
"compilerOptions": { "outDir": "./dist" }
}

0 comments on commit 16aae73

Please sign in to comment.