From ef1ad16146d6268e00ce31c94d59bd7b0037df95 Mon Sep 17 00:00:00 2001 From: jpb06 Date: Tue, 19 Mar 2024 00:10:10 +0100 Subject: [PATCH] chore: using genericity for pages aggregators --- .../aggregators/get-pull-request-reviews.ts | 20 +------- .../aggregators/get-repo-issues.ts | 22 ++------- .../aggregators/get-repo-pull-requests.ts | 22 ++------- .../aggregators/get-repositories.ts | 22 ++------- .../aggregators/get-user-events.ts | 20 +------- .../constants/default-retry-count.constant.ts | 1 - .../generic/get-all-pages.effect.ts | 48 +++++++++++++++++++ src/layer/octokit.layer.ts | 2 - 8 files changed, 64 insertions(+), 93 deletions(-) delete mode 100644 src/layer/github/implementation/constants/default-retry-count.constant.ts create mode 100644 src/layer/github/implementation/generic/get-all-pages.effect.ts diff --git a/src/layer/github/implementation/aggregators/get-pull-request-reviews.ts b/src/layer/github/implementation/aggregators/get-pull-request-reviews.ts index 0191571..085690f 100644 --- a/src/layer/github/implementation/aggregators/get-pull-request-reviews.ts +++ b/src/layer/github/implementation/aggregators/get-pull-request-reviews.ts @@ -1,8 +1,7 @@ import { Effect } from 'effect'; import { EffectResultSuccess } from '../../../../types/effect.types'; -import { arrayRange } from '../../../../util/array-range.util'; -import { defaultConcurrency } from '../constants/default-concurrency.constant'; +import { getAllPages } from '../generic/get-all-pages.effect'; import { getPullRequestReviewsPage } from '../paging/get-pull-request-reviews-page'; export interface GetPullRequestReviewsArgs { @@ -21,22 +20,7 @@ const getPage = (args: GetPullRequestReviewsArgs) => (page: number) => export const getPullRequestReviews = (args: GetPullRequestReviewsArgs) => Effect.withSpan(__filename, { attributes: { ...args }, - })( - Effect.gen(function* (_) { - const firstPage = yield* _(getPage(args)(1)); - if (firstPage.links?.last === undefined) { - return firstPage.data; - } - - const pagesResults = yield* _( - Effect.all(arrayRange(2, firstPage.links.last).map(getPage(args)), { - concurrency: args.concurrency ?? defaultConcurrency, - }), - ); - - return [...firstPage.data, ...pagesResults.flatMap((r) => r.data)]; - }), - ); + })(getAllPages(getPage, args)); export type PullRequestReviewsResult = EffectResultSuccess< typeof getPullRequestReviews diff --git a/src/layer/github/implementation/aggregators/get-repo-issues.ts b/src/layer/github/implementation/aggregators/get-repo-issues.ts index 378ad31..5f367bd 100644 --- a/src/layer/github/implementation/aggregators/get-repo-issues.ts +++ b/src/layer/github/implementation/aggregators/get-repo-issues.ts @@ -1,8 +1,7 @@ import { Effect } from 'effect'; import { EffectResultSuccess } from '../../../../types/effect.types'; -import { arrayRange } from '../../../../util/array-range.util'; -import { defaultConcurrency } from '../constants/default-concurrency.constant'; +import { getAllPages } from '../generic/get-all-pages.effect'; import { getRepoIssuesPage } from '../paging/get-repo-issues-page'; export interface GetRepoIssuesArgs { @@ -18,21 +17,8 @@ const getPage = (args: GetRepoIssuesArgs) => (page: number) => }); export const getRepoIssues = (args: GetRepoIssuesArgs) => - Effect.withSpan(__filename, { attributes: { ...args } })( - Effect.gen(function* (_) { - const firstPage = yield* _(getPage(args)(1)); - if (firstPage.links?.last === undefined) { - return firstPage.data; - } - - const pagesResults = yield* _( - Effect.all(arrayRange(2, firstPage.links.last).map(getPage(args)), { - concurrency: args.concurrency ?? defaultConcurrency, - }), - ); - - return [...firstPage.data, ...pagesResults.flatMap((r) => r.data)]; - }), - ); + Effect.withSpan(__filename, { + attributes: { ...args }, + })(getAllPages(getPage, args)); export type RepoIssuesResult = EffectResultSuccess; diff --git a/src/layer/github/implementation/aggregators/get-repo-pull-requests.ts b/src/layer/github/implementation/aggregators/get-repo-pull-requests.ts index f1fce6b..33bb47e 100644 --- a/src/layer/github/implementation/aggregators/get-repo-pull-requests.ts +++ b/src/layer/github/implementation/aggregators/get-repo-pull-requests.ts @@ -1,8 +1,7 @@ import { Effect } from 'effect'; import { EffectResultSuccess } from '../../../../types/effect.types'; -import { arrayRange } from '../../../../util/array-range.util'; -import { defaultConcurrency } from '../constants/default-concurrency.constant'; +import { getAllPages } from '../generic/get-all-pages.effect'; import { getRepoPullRequestsPage } from '../paging/get-repo-pull-requests-page'; export interface GetRepoPullRequestsArgs { @@ -18,22 +17,9 @@ const getPage = (args: GetRepoPullRequestsArgs) => (page: number) => }); export const getRepoPullRequests = (args: GetRepoPullRequestsArgs) => - Effect.withSpan(__filename, { attributes: { ...args } })( - Effect.gen(function* (_) { - const firstPage = yield* _(getPage(args)(1)); - if (firstPage.links?.last === undefined) { - return firstPage.data; - } - - const pagesResults = yield* _( - Effect.all(arrayRange(2, firstPage.links.last).map(getPage(args)), { - concurrency: args.concurrency ?? defaultConcurrency, - }), - ); - - return [...firstPage.data, ...pagesResults.flatMap((r) => r.data)]; - }), - ); + Effect.withSpan(__filename, { + attributes: { ...args }, + })(getAllPages(getPage, args)); export type RepoPullRequestsResult = EffectResultSuccess< typeof getRepoPullRequests diff --git a/src/layer/github/implementation/aggregators/get-repositories.ts b/src/layer/github/implementation/aggregators/get-repositories.ts index 8173dc2..23fb4c0 100644 --- a/src/layer/github/implementation/aggregators/get-repositories.ts +++ b/src/layer/github/implementation/aggregators/get-repositories.ts @@ -2,8 +2,7 @@ import { Effect } from 'effect'; import { match } from 'ts-pattern'; import { EffectResultSuccess } from '../../../../types/effect.types'; -import { arrayRange } from '../../../../util/array-range.util'; -import { defaultConcurrency } from '../constants/default-concurrency.constant'; +import { getAllPages } from '../generic/get-all-pages.effect'; import { getOrgReposPage } from '../paging/get-org-repos-page'; import { getUserReposPage } from '../paging/get-user-repos-page'; @@ -32,21 +31,8 @@ const getPage = .exhaustive(); export const getRepositories = (args: GetRepositoriesArgs) => - Effect.withSpan(__filename, { attributes: { ...args } })( - Effect.gen(function* (_) { - const firstPage = yield* _(getPage(args)(1)); - if (firstPage.links?.last === undefined) { - return firstPage.data; - } - - const pagesResults = yield* _( - Effect.all(arrayRange(2, firstPage.links.last).map(getPage(args)), { - concurrency: args.concurrency ?? defaultConcurrency, - }), - ); - - return [...firstPage.data, ...pagesResults.flatMap((r) => r.data)]; - }), - ); + Effect.withSpan(__filename, { + attributes: { ...args }, + })(getAllPages(getPage, args)); export type RepositoriesResult = EffectResultSuccess; diff --git a/src/layer/github/implementation/aggregators/get-user-events.ts b/src/layer/github/implementation/aggregators/get-user-events.ts index 3efa2d7..c3a7707 100644 --- a/src/layer/github/implementation/aggregators/get-user-events.ts +++ b/src/layer/github/implementation/aggregators/get-user-events.ts @@ -1,8 +1,7 @@ import { Effect } from 'effect'; import { EffectResultSuccess } from '../../../..'; -import { arrayRange } from '../../../../util/array-range.util'; -import { defaultConcurrency } from '../constants/default-concurrency.constant'; +import { getAllPages } from '../generic/get-all-pages.effect'; import { getUserEventsPage } from '../paging/get-user-events-page'; export interface GetUserEventsArgs { @@ -19,21 +18,6 @@ const getPage = (args: GetUserEventsArgs) => (page: number) => export const getUserEvents = (args: GetUserEventsArgs) => Effect.withSpan(__filename, { attributes: { ...args }, - })( - Effect.gen(function* (_) { - const firstPage = yield* _(getPage(args)(1)); - if (firstPage.links?.last === undefined) { - return firstPage.data; - } - - const pagesResults = yield* _( - Effect.all(arrayRange(2, firstPage.links.last).map(getPage(args)), { - concurrency: args.concurrency ?? defaultConcurrency, - }), - ); - - return [...firstPage.data, ...pagesResults.flatMap((r) => r.data)]; - }), - ); + })(getAllPages(getPage, args)); export type UserEventsResult = EffectResultSuccess; diff --git a/src/layer/github/implementation/constants/default-retry-count.constant.ts b/src/layer/github/implementation/constants/default-retry-count.constant.ts deleted file mode 100644 index fedc885..0000000 --- a/src/layer/github/implementation/constants/default-retry-count.constant.ts +++ /dev/null @@ -1 +0,0 @@ -export const defaultRetryCount = 1; diff --git a/src/layer/github/implementation/generic/get-all-pages.effect.ts b/src/layer/github/implementation/generic/get-all-pages.effect.ts new file mode 100644 index 0000000..37afceb --- /dev/null +++ b/src/layer/github/implementation/generic/get-all-pages.effect.ts @@ -0,0 +1,48 @@ +import { Effect } from 'effect'; + +import { arrayRange } from '../../../../util/array-range.util'; +import { defaultConcurrency } from '../constants/default-concurrency.constant'; + +type LinkKey = 'prev' | 'next' | 'last'; + +interface DataWithLinks { + links: Record | undefined; + data: TData; +} + +type GetPage = ( + args: TArgs, +) => (page: number) => Effect.Effect, TError>; + +export const getAllPages = < + TError, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + TArgs extends { concurrency?: number } & Record, + TDataItem, + TData extends TDataItem[], +>( + getPage: GetPage, + args: TArgs, +) => + Effect.withSpan(__filename, { + attributes: { ...args }, + })( + Effect.gen(function* (_) { + const firstPage = yield* _(getPage(args)(1)); + + if (firstPage.links?.last === undefined) { + return firstPage.data; + } + + const pagesResults = yield* _( + Effect.all(arrayRange(2, firstPage.links.last).map(getPage(args)), { + concurrency: args.concurrency ?? defaultConcurrency, + }), + ); + + return [ + ...firstPage.data, + ...pagesResults.flatMap((r) => r.data), + ] as TData; + }), + ); diff --git a/src/layer/octokit.layer.ts b/src/layer/octokit.layer.ts index e2289bd..a180836 100644 --- a/src/layer/octokit.layer.ts +++ b/src/layer/octokit.layer.ts @@ -57,5 +57,3 @@ export const OctokitLayer = { }), }), }; - -OctokitLayer.repo({ owner: 'jpb06', name: 'effect' }).pull(1).reviews();