@inixiative/integrations 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -13
- package/package.json +3 -2
- package/src/catalog/catalogOperationMeta.ts +4 -0
- package/src/catalog/catalogOperations.ts +10 -0
- package/src/catalog/providerModel.ts +0 -1
- package/src/contracts.ts +1 -10
- package/src/credentials/credentialAuth.ts +38 -0
- package/src/credentials/credentialClient.ts +2 -2
- package/src/credentials/exchangeOauthCode.ts +30 -36
- package/src/credentials/mintProviderCredential.ts +26 -40
- package/src/credentials/providerExchangeTimeoutMs.ts +1 -1
- package/src/credentials/refreshProviderCredential.ts +62 -65
- package/src/credentials/renewalError.ts +11 -0
- package/src/credentials/requestOauthToken.ts +28 -0
- package/src/credentials/validateCredentialSecret.ts +5 -5
- package/src/index.ts +0 -1
- package/src/operations/providerOperationInput.ts +6 -0
- package/src/operations/providerOperationInputSchema.ts +36 -0
- package/src/operations/providerOperationOrThrow.ts +23 -0
- package/src/operations/providerOperationSet.ts +7 -0
- package/src/operations/providerOperations.ts +38 -0
- package/src/providers/confluence/confluenceApiBaseUrl.ts +1 -0
- package/src/providers/confluence/confluenceOperations.ts +4 -0
- package/src/providers/confluence/confluenceProvider.ts +5 -8
- package/src/providers/confluence/models/page/fetchConfluencePage.ts +23 -0
- package/src/providers/discord/discordApiBaseUrl.ts +1 -0
- package/src/providers/discord/discordOperations.ts +8 -0
- package/src/providers/discord/discordProvider.ts +5 -14
- package/src/providers/discord/models/channel/discordChannelKind.ts +1 -0
- package/src/providers/discord/models/channel/fetchDiscordChannel.ts +25 -0
- package/src/providers/discord/models/channel/fetchDiscordChannelMessages.ts +37 -0
- package/src/providers/drive/driveApiBaseUrl.ts +1 -0
- package/src/providers/drive/driveOperations.ts +4 -0
- package/src/providers/drive/driveProvider.ts +5 -8
- package/src/providers/drive/models/file/fetchDriveFile.ts +27 -0
- package/src/providers/github/auth/createGithubInstallationToken.ts +19 -0
- package/src/providers/github/auth/fetchGithubAppInstallUrl.ts +21 -0
- package/src/providers/github/auth/fetchGithubAppInstallation.ts +16 -19
- package/src/providers/github/auth/fetchGithubOrganizationRole.ts +22 -22
- package/src/providers/github/auth/fetchGithubUser.ts +10 -4
- package/src/providers/github/auth/githubAppAuth.ts +6 -0
- package/src/providers/github/auth/listGithubUserInstallationPage.ts +18 -0
- package/src/providers/github/events/listGithubNotificationPage.ts +18 -0
- package/src/providers/github/githubApiBaseUrl.ts +1 -0
- package/src/providers/github/githubOrganizationProvider.ts +5 -1
- package/src/providers/github/githubPagination.ts +2 -2
- package/src/providers/github/githubRequestHeaders.ts +4 -6
- package/src/providers/github/githubUserProvider.ts +5 -2
- package/src/providers/github/models/account/githubAccount.ts +0 -1
- package/src/providers/github/models/repository/fetchGithubBlob.ts +59 -0
- package/src/providers/github/models/repository/fetchGithubCommit.ts +38 -0
- package/src/providers/github/models/repository/fetchGithubTree.ts +74 -0
- package/src/providers/github/models/repository/githubObjectRequest.ts +15 -0
- package/src/providers/github/models/repository/githubRepository.ts +1 -11
- package/src/providers/github/models/repository/githubRepositoryOperations.ts +10 -0
- package/src/providers/github/models/repository/listGithubInstallationRepositoryPage.ts +18 -0
- package/src/providers/github/models/repository/listGithubUserInstallationRepositoryPage.ts +22 -0
- package/src/providers/github/models/repository/readGithubObjectIdentity.ts +9 -0
- package/src/providers/github/services/fetchGithubUserInstallation.ts +25 -0
- package/src/providers/github/services/listGithubNotifications.ts +27 -0
- package/src/providers/github/services/listInstallationRepositories.ts +34 -0
- package/src/providers/github/services/listUserRepositories.ts +43 -0
- package/src/providers/gmail/gmailApiBaseUrl.ts +1 -0
- package/src/providers/gmail/gmailOperations.ts +10 -0
- package/src/providers/gmail/gmailProvider.ts +6 -9
- package/src/providers/gmail/models/label/listGmailLabelMessages.ts +45 -0
- package/src/providers/gmail/models/message/fetchGmailMessage.ts +19 -0
- package/src/providers/gmail/models/message/fetchGmailMessageBatch.ts +20 -0
- package/src/providers/gmail/models/message/gmailMessageKind.ts +1 -0
- package/src/providers/gmail/models/message/gmailMessageRequest.ts +9 -0
- package/src/providers/gmail/models/message/readGmailMessage.ts +26 -0
- package/src/providers/google/auth/verifyGoogleIdToken.ts +3 -3
- package/src/providers/jira/jiraApiBaseUrl.ts +1 -0
- package/src/providers/jira/jiraOperations.ts +4 -0
- package/src/providers/jira/jiraProvider.ts +5 -8
- package/src/providers/jira/models/issue/fetchJiraIssue.ts +22 -0
- package/src/providers/linear/events/createLinearWebhook.ts +29 -20
- package/src/providers/linear/events/deleteLinearWebhook.ts +16 -9
- package/src/providers/linear/events/linearWebhook.ts +1 -1
- package/src/providers/linear/events/listLinearWebhookPage.ts +23 -0
- package/src/providers/linear/events/repairLinearWebhook.ts +17 -13
- package/src/providers/linear/linearApiBaseUrl.ts +1 -0
- package/src/providers/linear/linearGraphqlErrorCode.ts +8 -0
- package/src/providers/linear/linearOperations.ts +4 -0
- package/src/providers/linear/linearPagination.ts +3 -9
- package/src/providers/linear/linearProvider.ts +5 -2
- package/src/providers/linear/models/issue/fetchLinearIssue.ts +32 -0
- package/src/providers/linear/models/team/fetchLinearWorkspace.ts +23 -13
- package/src/providers/linear/models/team/linearTeam.ts +0 -1
- package/src/providers/linear/services/listLinearWebhooks.ts +45 -0
- package/src/providers/notion/models/page/fetchNotionPage.ts +32 -0
- package/src/providers/notion/models/page/fetchNotionPageBlocks.ts +39 -0
- package/src/providers/notion/models/page/notionPageKind.ts +1 -0
- package/src/providers/notion/notionApiBaseUrl.ts +1 -0
- package/src/providers/notion/notionOperations.ts +8 -0
- package/src/providers/notion/notionProvider.ts +5 -14
- package/src/providers/notion/notionVersion.ts +1 -0
- package/src/providers/slack/auth/fetchSlackIdentity.ts +14 -6
- package/src/providers/slack/models/account/slackAccount.ts +0 -1
- package/src/providers/slack/models/channel/fetchSlackChannel.ts +32 -0
- package/src/providers/slack/models/channel/fetchSlackChannelMessages.ts +34 -0
- package/src/providers/slack/models/channel/listSlackUserConversations.ts +19 -0
- package/src/providers/slack/models/channel/slackChannel.ts +0 -14
- package/src/providers/slack/readSlackBody.ts +10 -0
- package/src/providers/slack/services/listSlackChannels.ts +23 -0
- package/src/providers/slack/slackApiBaseUrl.ts +1 -0
- package/src/providers/slack/slackOperations.ts +8 -0
- package/src/providers/slack/slackPagination.ts +2 -2
- package/src/providers/slack/slackProvider.ts +9 -1
- package/src/providers/slack/slackProviderError.ts +2 -2
- package/src/providers/teams/models/channel/fetchTeamsChannelMessages.ts +36 -0
- package/src/providers/teams/teamsApiBaseUrl.ts +1 -0
- package/src/providers/teams/teamsOperations.ts +6 -0
- package/src/providers/teams/teamsProvider.ts +5 -8
- package/src/utils/acceptJsonHeaders.ts +1 -0
- package/src/utils/pageQuerySchema.ts +13 -0
- package/src/utils/readProviderLimits.ts +1 -2
- package/src/credentials/fetchRenewalResponse.ts +0 -41
- package/src/credentials/renewalRequest.ts +0 -1
- package/src/operations/buildProviderRequest.ts +0 -55
- package/src/operations/executeProvider.ts +0 -26
- package/src/operations/findOperationHandler.ts +0 -6
- package/src/operations/operationHandler.ts +0 -8
- package/src/operations/operationHandlers.ts +0 -39
- package/src/operations/operationRequestInput.ts +0 -12
- package/src/operations/providerRequestSpec.ts +0 -1
- package/src/providers/confluence/confluenceOperationHandler.ts +0 -14
- package/src/providers/discord/discordOperationHandler.ts +0 -30
- package/src/providers/drive/driveOperationHandler.ts +0 -21
- package/src/providers/github/auth/fetchGithubUserInstallation.ts +0 -21
- package/src/providers/github/auth/githubAppClient.ts +0 -4
- package/src/providers/github/auth/githubAppInstallUrl.ts +0 -20
- package/src/providers/github/auth/githubClientForApp.ts +0 -11
- package/src/providers/github/events/listGithubNotifications.ts +0 -27
- package/src/providers/github/fetchAllGithubPages.ts +0 -12
- package/src/providers/github/iterateGithubPages.ts +0 -12
- package/src/providers/github/models/repository/githubOperationHandler.ts +0 -36
- package/src/providers/github/models/repository/listInstallationRepositories.ts +0 -31
- package/src/providers/github/models/repository/listUserRepositories.ts +0 -36
- package/src/providers/github/models/repository/readGithubObject.ts +0 -85
- package/src/providers/github/requestGithub.ts +0 -18
- package/src/providers/gmail/buildGmailMessageRequest.ts +0 -22
- package/src/providers/gmail/fetchGmailMessages.ts +0 -81
- package/src/providers/gmail/gmailOperationHandler.ts +0 -12
- package/src/providers/jira/jiraOperationHandler.ts +0 -14
- package/src/providers/linear/events/listLinearWebhooks.ts +0 -44
- package/src/providers/linear/fetchAllLinearPages.ts +0 -40
- package/src/providers/linear/linearClient.ts +0 -5
- package/src/providers/linear/linearRequestInput.ts +0 -1
- package/src/providers/linear/models/issue/linearIssue.ts +0 -11
- package/src/providers/linear/models/issue/linearOperationHandler.ts +0 -30
- package/src/providers/linear/requestLinear.ts +0 -51
- package/src/providers/notion/notionOperationHandler.ts +0 -32
- package/src/providers/slack/fetchAllSlackPages.ts +0 -12
- package/src/providers/slack/models/channel/listSlackChannels.ts +0 -26
- package/src/providers/slack/models/channel/slackOperationHandler.ts +0 -30
- package/src/providers/slack/requestSlack.ts +0 -22
- package/src/providers/teams/teamsOperationHandler.ts +0 -24
- package/src/utils/headerNumber.ts +0 -6
- package/src/utils/pagination/applyCaps.ts +0 -18
- package/src/utils/pagination/byCursorKeys.ts +0 -7
- package/src/utils/pagination/byCursorOptions.ts +0 -7
- package/src/utils/pagination/byPageKeys.ts +0 -7
- package/src/utils/pagination/byPageOptions.ts +0 -8
- package/src/utils/pagination/collectPages.ts +0 -27
- package/src/utils/pagination/compactRequest.ts +0 -4
- package/src/utils/pagination/fetchAllByCursor.ts +0 -6
- package/src/utils/pagination/fetchAllByPage.ts +0 -6
- package/src/utils/pagination/fetchAllRequestPages.ts +0 -16
- package/src/utils/pagination/incompletePageWalkError.ts +0 -8
- package/src/utils/pagination/isAnotherPage.ts +0 -20
- package/src/utils/pagination/iterateByCursor.ts +0 -49
- package/src/utils/pagination/iterateByPage.ts +0 -59
- package/src/utils/pagination/iterateProviderPages.ts +0 -27
- package/src/utils/pagination/iterateRequestPages.ts +0 -18
- package/src/utils/pagination/pageWalkError.ts +0 -6
- package/src/utils/pagination/paginationOptions.ts +0 -9
- package/src/utils/pagination/providerPagesOptions.ts +0 -8
- package/src/utils/pagination/providerPagination.ts +0 -6
- package/src/utils/pagination/readPage.ts +0 -29
- package/src/utils/pagination/readPath.ts +0 -12
- package/src/utils/pagination/repeatedCursorError.ts +0 -10
- package/src/utils/providerClient.ts +0 -3
- package/src/utils/providerError.ts +0 -10
- package/src/utils/providerRequest.ts +0 -22
- package/src/utils/providerRequestInput.ts +0 -7
- package/src/utils/providerResponseLimit.ts +0 -1
- package/src/utils/providerSend.ts +0 -1
- package/src/utils/readProviderJson.ts +0 -38
- package/src/utils/readRateLimit.ts +0 -29
- package/src/utils/requestProviderJson.ts +0 -13
- package/src/utils/responseBudget.ts +0 -1
- package/src/utils/sendProviderRequest.ts +0 -33
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ByPageKeys } from './byPageKeys';
|
|
2
|
-
import type { PaginationOptions } from './paginationOptions';
|
|
3
|
-
|
|
4
|
-
export type ByPageOptions<R, T> = PaginationOptions<R> & {
|
|
5
|
-
keys: ByPageKeys;
|
|
6
|
-
hasMore?: (response: R, items: T[], page: number) => boolean;
|
|
7
|
-
itemKey?: (item: T) => string;
|
|
8
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { PaginationOptions } from './paginationOptions';
|
|
2
|
-
|
|
3
|
-
export const collectPages = async <
|
|
4
|
-
T,
|
|
5
|
-
O extends Pick<PaginationOptions<never>, 'maxRows' | 'onCapped'>,
|
|
6
|
-
>(
|
|
7
|
-
iterate: (options: O) => AsyncIterable<{ items: T[] }>,
|
|
8
|
-
options: O,
|
|
9
|
-
): Promise<T[]> => {
|
|
10
|
-
let capReason: 'maxPages' | 'maxRows' | undefined;
|
|
11
|
-
const items: T[] = [];
|
|
12
|
-
const pages = iterate({
|
|
13
|
-
...options,
|
|
14
|
-
onCapped: ({ reason }: { reason: 'maxPages' | 'maxRows' }) => {
|
|
15
|
-
capReason = reason;
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
for await (const walked of pages) items.push(...walked.items);
|
|
19
|
-
|
|
20
|
-
const maxRows = options.maxRows ?? Number.POSITIVE_INFINITY;
|
|
21
|
-
if (items.length > maxRows) {
|
|
22
|
-
items.length = maxRows;
|
|
23
|
-
capReason = 'maxRows';
|
|
24
|
-
}
|
|
25
|
-
if (capReason) options.onCapped?.({ reason: capReason, returned: items.length });
|
|
26
|
-
return items;
|
|
27
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ByCursorOptions } from './byCursorOptions';
|
|
2
|
-
import { collectPages } from './collectPages';
|
|
3
|
-
import { iterateByCursor } from './iterateByCursor';
|
|
4
|
-
|
|
5
|
-
export const fetchAllByCursor = <R, T = unknown>(options: ByCursorOptions<R>): Promise<T[]> =>
|
|
6
|
-
collectPages<T, ByCursorOptions<R>>(iterateByCursor<R, T>, options);
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ByPageOptions } from './byPageOptions';
|
|
2
|
-
import { collectPages } from './collectPages';
|
|
3
|
-
import { iterateByPage } from './iterateByPage';
|
|
4
|
-
|
|
5
|
-
export const fetchAllByPage = <R, T = unknown>(options: ByPageOptions<R, T>): Promise<T[]> =>
|
|
6
|
-
collectPages<T, ByPageOptions<R, T>>(iterateByPage<R, T>, options);
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { ProviderRequestInput } from '../providerRequestInput';
|
|
2
|
-
import { collectPages } from './collectPages';
|
|
3
|
-
import { iterateRequestPages } from './iterateRequestPages';
|
|
4
|
-
import type { ProviderPagesOptions } from './providerPagesOptions';
|
|
5
|
-
import type { ProviderPagination } from './providerPagination';
|
|
6
|
-
|
|
7
|
-
export const fetchAllRequestPages = (
|
|
8
|
-
pagination: ProviderPagination,
|
|
9
|
-
request: (input: ProviderRequestInput) => Promise<unknown>,
|
|
10
|
-
input: ProviderRequestInput,
|
|
11
|
-
options: ProviderPagesOptions = {},
|
|
12
|
-
) =>
|
|
13
|
-
collectPages(
|
|
14
|
-
(walk: ProviderPagesOptions) => iterateRequestPages(pagination, request, input, walk),
|
|
15
|
-
options,
|
|
16
|
-
);
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { ByPageKeys } from './byPageKeys';
|
|
2
|
-
import { readPath } from './readPath';
|
|
3
|
-
|
|
4
|
-
export const isAnotherPage = (
|
|
5
|
-
response: unknown,
|
|
6
|
-
itemCount: number,
|
|
7
|
-
{
|
|
8
|
-
keys,
|
|
9
|
-
pageSize,
|
|
10
|
-
progress,
|
|
11
|
-
}: { keys: ByPageKeys; pageSize?: number; progress: { pages: number; rows: number } },
|
|
12
|
-
): boolean => {
|
|
13
|
-
if (itemCount === 0) return false;
|
|
14
|
-
const totalPages =
|
|
15
|
-
keys.totalPages === undefined ? undefined : readPath(response, keys.totalPages);
|
|
16
|
-
if (typeof totalPages === 'number') return progress.pages < totalPages;
|
|
17
|
-
const total = keys.total === undefined ? undefined : readPath(response, keys.total);
|
|
18
|
-
if (typeof total === 'number') return progress.rows < total;
|
|
19
|
-
return pageSize !== undefined && itemCount >= pageSize;
|
|
20
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { applyCaps } from './applyCaps';
|
|
2
|
-
import type { ByCursorOptions } from './byCursorOptions';
|
|
3
|
-
import { compactRequest } from './compactRequest';
|
|
4
|
-
import { readPage } from './readPage';
|
|
5
|
-
import { readPath } from './readPath';
|
|
6
|
-
import { RepeatedCursorError } from './repeatedCursorError';
|
|
7
|
-
|
|
8
|
-
const label = 'iterateByCursor';
|
|
9
|
-
|
|
10
|
-
export const iterateByCursor = <R, T = unknown>(options: ByCursorOptions<R>) => ({
|
|
11
|
-
async *[Symbol.asyncIterator](): AsyncGenerator<
|
|
12
|
-
{ items: T[]; page: number; cursor: string | null; nextCursor: string | null },
|
|
13
|
-
void
|
|
14
|
-
> {
|
|
15
|
-
const { keys } = options;
|
|
16
|
-
const progress = { pages: 0, rows: 0 };
|
|
17
|
-
let cursor = options.startCursor || null;
|
|
18
|
-
const sent = new Set<string>();
|
|
19
|
-
|
|
20
|
-
for (;;) {
|
|
21
|
-
if (cursor !== null) sent.add(cursor);
|
|
22
|
-
const request = compactRequest({
|
|
23
|
-
[keys.cursor]: cursor,
|
|
24
|
-
[keys.pageSize]: options.pageSize,
|
|
25
|
-
});
|
|
26
|
-
const { response, items } = await readPage<R, T>(
|
|
27
|
-
options,
|
|
28
|
-
request,
|
|
29
|
-
progress,
|
|
30
|
-
label,
|
|
31
|
-
keys.data,
|
|
32
|
-
);
|
|
33
|
-
const next = readPath(response, keys.nextCursor);
|
|
34
|
-
const isLast =
|
|
35
|
-
next === undefined ||
|
|
36
|
-
next === null ||
|
|
37
|
-
next === '' ||
|
|
38
|
-
(keys.hasMore !== undefined && readPath(response, keys.hasMore) === false);
|
|
39
|
-
const nextCursor = isLast ? null : String(next);
|
|
40
|
-
if (nextCursor !== null && sent.has(nextCursor))
|
|
41
|
-
throw new RepeatedCursorError(label, progress.pages, nextCursor);
|
|
42
|
-
|
|
43
|
-
const isCapped = nextCursor !== null && applyCaps(options, progress);
|
|
44
|
-
yield { items, page: progress.pages, cursor, nextCursor };
|
|
45
|
-
if (nextCursor === null || isCapped) return;
|
|
46
|
-
cursor = nextCursor;
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
});
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { applyCaps } from './applyCaps';
|
|
2
|
-
import type { ByPageOptions } from './byPageOptions';
|
|
3
|
-
import { compactRequest } from './compactRequest';
|
|
4
|
-
import { IncompletePageWalkError } from './incompletePageWalkError';
|
|
5
|
-
import { isAnotherPage } from './isAnotherPage';
|
|
6
|
-
import { readPage } from './readPage';
|
|
7
|
-
import { readPath } from './readPath';
|
|
8
|
-
|
|
9
|
-
const label = 'iterateByPage';
|
|
10
|
-
|
|
11
|
-
export const iterateByPage = <R, T = unknown>(options: ByPageOptions<R, T>) => ({
|
|
12
|
-
async *[Symbol.asyncIterator](): AsyncGenerator<{ items: T[]; page: number }, void> {
|
|
13
|
-
const { keys } = options;
|
|
14
|
-
const progress = { pages: 0, rows: 0 };
|
|
15
|
-
const seenKeys = new Set<string>();
|
|
16
|
-
let firstTotal: number | undefined;
|
|
17
|
-
|
|
18
|
-
for (;;) {
|
|
19
|
-
const request = compactRequest({
|
|
20
|
-
[keys.page]: progress.pages + 1,
|
|
21
|
-
[keys.pageSize]: options.pageSize,
|
|
22
|
-
});
|
|
23
|
-
const { response, items } = await readPage<R, T>(
|
|
24
|
-
options,
|
|
25
|
-
request,
|
|
26
|
-
progress,
|
|
27
|
-
label,
|
|
28
|
-
keys.data,
|
|
29
|
-
);
|
|
30
|
-
const page = progress.pages;
|
|
31
|
-
if (options.itemKey) {
|
|
32
|
-
if (keys.total === undefined)
|
|
33
|
-
throw new IncompletePageWalkError(label, 'itemKey needs a total key');
|
|
34
|
-
const total = readPath(response, keys.total);
|
|
35
|
-
if (typeof total !== 'number')
|
|
36
|
-
throw new IncompletePageWalkError(label, `page ${page} has no total at "${keys.total}"`);
|
|
37
|
-
firstTotal ??= total;
|
|
38
|
-
if (total !== firstTotal)
|
|
39
|
-
throw new IncompletePageWalkError(
|
|
40
|
-
label,
|
|
41
|
-
`page ${page} reports a total of ${total}, page 1 reported ${firstTotal}`,
|
|
42
|
-
);
|
|
43
|
-
for (const item of items) seenKeys.add(options.itemKey(item));
|
|
44
|
-
}
|
|
45
|
-
const isAnother = options.hasMore
|
|
46
|
-
? options.hasMore(response, items, page)
|
|
47
|
-
: isAnotherPage(response, items.length, { keys, pageSize: options.pageSize, progress });
|
|
48
|
-
|
|
49
|
-
const isCapped = isAnother && applyCaps(options, progress);
|
|
50
|
-
if (options.itemKey && !isAnother && seenKeys.size !== firstTotal)
|
|
51
|
-
throw new IncompletePageWalkError(
|
|
52
|
-
label,
|
|
53
|
-
`the walk read ${seenKeys.size} distinct items, page 1 reported a total of ${firstTotal}`,
|
|
54
|
-
);
|
|
55
|
-
yield { items, page };
|
|
56
|
-
if (!isAnother || isCapped) return;
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
});
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { ProviderError } from '../providerError';
|
|
2
|
-
import type { ByCursorKeys } from './byCursorKeys';
|
|
3
|
-
import type { ByPageKeys } from './byPageKeys';
|
|
4
|
-
import { iterateByCursor } from './iterateByCursor';
|
|
5
|
-
import { iterateByPage } from './iterateByPage';
|
|
6
|
-
import { PageWalkError } from './pageWalkError';
|
|
7
|
-
import type { PaginationOptions } from './paginationOptions';
|
|
8
|
-
import type { ProviderPagination } from './providerPagination';
|
|
9
|
-
|
|
10
|
-
export const iterateProviderPages = <R, T = unknown>(
|
|
11
|
-
pagination: ProviderPagination,
|
|
12
|
-
{ keys, ...options }: PaginationOptions<R> & { keys?: Partial<ByPageKeys & ByCursorKeys> },
|
|
13
|
-
) => ({
|
|
14
|
-
async *[Symbol.asyncIterator](): AsyncGenerator<{ items: T[]; page: number }, void> {
|
|
15
|
-
const walk = { ...options, pageSize: options.pageSize ?? pagination.pageSize };
|
|
16
|
-
const pages =
|
|
17
|
-
pagination.strategy === 'page'
|
|
18
|
-
? iterateByPage<R, T>({ ...walk, keys: { ...pagination.keys, ...keys } })
|
|
19
|
-
: iterateByCursor<R, T>({ ...walk, keys: { ...pagination.keys, ...keys } });
|
|
20
|
-
try {
|
|
21
|
-
yield* pages;
|
|
22
|
-
} catch (error) {
|
|
23
|
-
if (error instanceof PageWalkError) throw new ProviderError('provider_invalid_response');
|
|
24
|
-
throw error;
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ProviderRequestInput } from '../providerRequestInput';
|
|
2
|
-
import { iterateProviderPages } from './iterateProviderPages';
|
|
3
|
-
import type { ProviderPagesOptions } from './providerPagesOptions';
|
|
4
|
-
import type { ProviderPagination } from './providerPagination';
|
|
5
|
-
|
|
6
|
-
export const iterateRequestPages = (
|
|
7
|
-
pagination: ProviderPagination,
|
|
8
|
-
request: (input: ProviderRequestInput) => Promise<unknown>,
|
|
9
|
-
input: ProviderRequestInput,
|
|
10
|
-
options: ProviderPagesOptions = {},
|
|
11
|
-
) => {
|
|
12
|
-
const pageSize = input.query?.[options.keys?.pageSize ?? pagination.keys.pageSize];
|
|
13
|
-
return iterateProviderPages(pagination, {
|
|
14
|
-
...options,
|
|
15
|
-
pageSize: pageSize === undefined ? undefined : Number(pageSize),
|
|
16
|
-
fetchPage: (page) => request({ ...input, query: { ...input.query, ...page } }),
|
|
17
|
-
});
|
|
18
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export type PaginationOptions<R> = {
|
|
2
|
-
fetchPage: (request: Record<string, string | number>, page: number) => Promise<R>;
|
|
3
|
-
pageSize?: number;
|
|
4
|
-
maxPages?: number;
|
|
5
|
-
maxRows?: number;
|
|
6
|
-
onCapped?: (cap: { reason: 'maxPages' | 'maxRows'; returned: number }) => void;
|
|
7
|
-
isError?: (response: R) => unknown;
|
|
8
|
-
onResponse?: (response: R, page: number) => void;
|
|
9
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ByCursorKeys } from './byCursorKeys';
|
|
2
|
-
import type { ByPageKeys } from './byPageKeys';
|
|
3
|
-
import type { PaginationOptions } from './paginationOptions';
|
|
4
|
-
|
|
5
|
-
export type ProviderPagesOptions = Pick<
|
|
6
|
-
PaginationOptions<unknown>,
|
|
7
|
-
'maxPages' | 'maxRows' | 'onCapped' | 'onResponse'
|
|
8
|
-
> & { keys?: Partial<ByPageKeys & ByCursorKeys> };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { PageWalkError } from './pageWalkError';
|
|
2
|
-
import type { PaginationOptions } from './paginationOptions';
|
|
3
|
-
import { readPath } from './readPath';
|
|
4
|
-
|
|
5
|
-
export const readPage = async <R, T>(
|
|
6
|
-
options: PaginationOptions<R>,
|
|
7
|
-
request: Record<string, string | number>,
|
|
8
|
-
progress: { pages: number; rows: number },
|
|
9
|
-
label: string,
|
|
10
|
-
dataKey: string,
|
|
11
|
-
): Promise<{ response: R; items: T[] }> => {
|
|
12
|
-
const page = progress.pages + 1;
|
|
13
|
-
const response = await options.fetchPage(request, page);
|
|
14
|
-
const requestError = options.isError?.(response);
|
|
15
|
-
if (requestError) {
|
|
16
|
-
const message = readPath(requestError, 'message');
|
|
17
|
-
throw new Error(
|
|
18
|
-
`${label}: page ${page} failed${message === undefined ? '' : `: ${String(message)}`}`,
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
options.onResponse?.(response, page);
|
|
22
|
-
|
|
23
|
-
const items = readPath(response, dataKey);
|
|
24
|
-
if (!Array.isArray(items))
|
|
25
|
-
throw new PageWalkError(`${label}: page ${page} has no array at "${dataKey}"`);
|
|
26
|
-
progress.pages = page;
|
|
27
|
-
progress.rows += items.length;
|
|
28
|
-
return { response, items };
|
|
29
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export const readPath = (value: unknown, path: string): unknown =>
|
|
2
|
-
path === ''
|
|
3
|
-
? value
|
|
4
|
-
: path
|
|
5
|
-
.split('.')
|
|
6
|
-
.reduce<unknown>(
|
|
7
|
-
(node, key) =>
|
|
8
|
-
typeof node === 'object' && node !== null
|
|
9
|
-
? (node as Record<string, unknown>)[key]
|
|
10
|
-
: undefined,
|
|
11
|
-
value,
|
|
12
|
-
);
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { PageWalkError } from './pageWalkError';
|
|
2
|
-
|
|
3
|
-
export class RepeatedCursorError extends PageWalkError {
|
|
4
|
-
constructor(label: string, page: number, cursor: string) {
|
|
5
|
-
super(
|
|
6
|
-
`${label}: page ${page} returned a cursor this walk already sent (${cursor}) as its next cursor`,
|
|
7
|
-
);
|
|
8
|
-
this.name = 'RepeatedCursorError';
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { ProviderError } from './providerError';
|
|
2
|
-
import type { ProviderRequestInput } from './providerRequestInput';
|
|
3
|
-
|
|
4
|
-
export const providerRequest = (
|
|
5
|
-
baseUrl: string,
|
|
6
|
-
{ path, method, query = {}, body }: ProviderRequestInput,
|
|
7
|
-
headers: Headers,
|
|
8
|
-
) => {
|
|
9
|
-
const url = new URL(path, baseUrl);
|
|
10
|
-
// why: the request carries the provider's token, so a path may never leave the provider's base.
|
|
11
|
-
if (!url.href.startsWith(new URL(baseUrl).href))
|
|
12
|
-
throw new ProviderError('unsupported_operation', 400);
|
|
13
|
-
for (const [key, value] of Object.entries(query)) url.searchParams.set(key, String(value));
|
|
14
|
-
if (body !== undefined) headers.set('Content-Type', 'application/json');
|
|
15
|
-
return new Request(url.toString(), {
|
|
16
|
-
method: method ?? (body === undefined ? 'GET' : 'POST'),
|
|
17
|
-
headers,
|
|
18
|
-
body: body === undefined ? undefined : JSON.stringify(body),
|
|
19
|
-
redirect: 'error',
|
|
20
|
-
signal: AbortSignal.timeout(15000),
|
|
21
|
-
});
|
|
22
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const providerResponseLimit = 1024 * 1024;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type ProviderSend = (request: Request) => Promise<Response>;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { ProviderError } from './providerError';
|
|
2
|
-
import type { ResponseBudget } from './responseBudget';
|
|
3
|
-
|
|
4
|
-
export const readProviderJson = async (
|
|
5
|
-
response: Response,
|
|
6
|
-
budget: ResponseBudget,
|
|
7
|
-
): Promise<unknown> => {
|
|
8
|
-
if (!response.body) throw new ProviderError('provider_empty_response');
|
|
9
|
-
const reader = response.body.getReader();
|
|
10
|
-
const chunks: Uint8Array[] = [];
|
|
11
|
-
let size = 0;
|
|
12
|
-
try {
|
|
13
|
-
while (true) {
|
|
14
|
-
const { done, value } = await reader.read();
|
|
15
|
-
if (done) break;
|
|
16
|
-
size += value.byteLength;
|
|
17
|
-
budget.remainingBytes -= value.byteLength;
|
|
18
|
-
if (budget.remainingBytes < 0) {
|
|
19
|
-
await reader.cancel();
|
|
20
|
-
throw new ProviderError('provider_response_too_large');
|
|
21
|
-
}
|
|
22
|
-
chunks.push(value);
|
|
23
|
-
}
|
|
24
|
-
} finally {
|
|
25
|
-
reader.releaseLock();
|
|
26
|
-
}
|
|
27
|
-
const bytes = new Uint8Array(size);
|
|
28
|
-
let offset = 0;
|
|
29
|
-
for (const chunk of chunks) {
|
|
30
|
-
bytes.set(chunk, offset);
|
|
31
|
-
offset += chunk.byteLength;
|
|
32
|
-
}
|
|
33
|
-
try {
|
|
34
|
-
return JSON.parse(new TextDecoder().decode(bytes));
|
|
35
|
-
} catch {
|
|
36
|
-
throw new ProviderError('provider_invalid_response');
|
|
37
|
-
}
|
|
38
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { headerNumber } from './headerNumber';
|
|
2
|
-
import type { ProviderReading } from './providerReading';
|
|
3
|
-
|
|
4
|
-
const retryAfterMs = (value: string | null, now: number): number | null => {
|
|
5
|
-
if (!value) return null;
|
|
6
|
-
const seconds = headerNumber(value);
|
|
7
|
-
if (seconds !== null) return Math.max(0, Math.round(seconds * 1000));
|
|
8
|
-
const instant = Date.parse(value);
|
|
9
|
-
return Number.isFinite(instant) ? Math.max(0, Math.round(instant - now)) : null;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// why: GitHub's primary limit answers 403 with `x-ratelimit-remaining: 0` and its reset in epoch seconds.
|
|
13
|
-
export const readRateLimit = (
|
|
14
|
-
headers: Headers,
|
|
15
|
-
status: number,
|
|
16
|
-
now = Date.now(),
|
|
17
|
-
): Pick<ProviderReading, 'rateLimited' | 'retryAfterMs'> => {
|
|
18
|
-
const exhausted = headerNumber(headers.get('x-ratelimit-remaining')) === 0;
|
|
19
|
-
const rateLimited =
|
|
20
|
-
status === 429 || (status === 403 && (exhausted || headers.has('retry-after')));
|
|
21
|
-
if (!rateLimited) return { rateLimited, retryAfterMs: null };
|
|
22
|
-
const reset = exhausted ? headerNumber(headers.get('x-ratelimit-reset')) : null;
|
|
23
|
-
return {
|
|
24
|
-
rateLimited,
|
|
25
|
-
retryAfterMs:
|
|
26
|
-
retryAfterMs(headers.get('retry-after'), now) ??
|
|
27
|
-
(reset === null ? null : Math.max(0, Math.round(reset * 1000 - now))),
|
|
28
|
-
};
|
|
29
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { providerResponseLimit } from './providerResponseLimit';
|
|
2
|
-
import type { ProviderSend } from './providerSend';
|
|
3
|
-
import { readProviderJson } from './readProviderJson';
|
|
4
|
-
import { sendProviderRequest } from './sendProviderRequest';
|
|
5
|
-
|
|
6
|
-
export const requestProviderJson = async (
|
|
7
|
-
send: ProviderSend,
|
|
8
|
-
request: Request,
|
|
9
|
-
options: { readableStatuses?: number[] } = {},
|
|
10
|
-
) =>
|
|
11
|
-
await readProviderJson(await sendProviderRequest(send, request, options), {
|
|
12
|
-
remainingBytes: providerResponseLimit,
|
|
13
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type ResponseBudget = { remainingBytes: number };
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { ProviderError } from './providerError';
|
|
2
|
-
import type { ProviderSend } from './providerSend';
|
|
3
|
-
import { readRateLimit } from './readRateLimit';
|
|
4
|
-
|
|
5
|
-
export const sendProviderRequest = async (
|
|
6
|
-
send: ProviderSend,
|
|
7
|
-
request: Request,
|
|
8
|
-
{ readableStatuses = [] }: { readableStatuses?: number[] } = {},
|
|
9
|
-
) => {
|
|
10
|
-
let response: Response;
|
|
11
|
-
try {
|
|
12
|
-
response = await send(request);
|
|
13
|
-
} catch {
|
|
14
|
-
throw new ProviderError('provider_unreachable');
|
|
15
|
-
}
|
|
16
|
-
if (!response.ok && !readableStatuses.includes(response.status)) {
|
|
17
|
-
await response.body?.cancel();
|
|
18
|
-
const rateLimit = readRateLimit(response.headers, response.status);
|
|
19
|
-
if (rateLimit.rateLimited)
|
|
20
|
-
throw new ProviderError('provider_rate_limited', 502, rateLimit.retryAfterMs);
|
|
21
|
-
throw new ProviderError(
|
|
22
|
-
response.status === 401
|
|
23
|
-
? 'provider_authentication_expired'
|
|
24
|
-
: response.status === 403
|
|
25
|
-
? 'provider_access_denied'
|
|
26
|
-
: response.status === 404
|
|
27
|
-
? 'provider_not_found'
|
|
28
|
-
: 'provider_failed',
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
if (!response.body) throw new ProviderError('provider_empty_response');
|
|
32
|
-
return response;
|
|
33
|
-
};
|