From 2178589507992c13289c1f5ff941feed6b4599f0 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Mon, 9 Dec 2024 18:58:57 +0800 Subject: [PATCH] fix: fix fetch official connector CLI error (#6862) --- .changeset/chilled-radios-cover.md | 9 +++++++ packages/cli/src/commands/connector/utils.ts | 25 ++++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 .changeset/chilled-radios-cover.md diff --git a/.changeset/chilled-radios-cover.md b/.changeset/chilled-radios-cover.md new file mode 100644 index 000000000..a3eb00abb --- /dev/null +++ b/.changeset/chilled-radios-cover.md @@ -0,0 +1,9 @@ +--- +"@logto/cli": patch +--- + +fix fetch official connector list CLI command error + +Due to changes in the npm registry API (`https://registry.npmjs.org/-/v1/search`) that our CLI add official connector depends on, the new API behavior returns irrelevant search results. + +We need to manually filter out these irrelevant results to avoid potential infinite loops, where each loop triggers an API call, eventually hitting a rate limit and resulting in a status code 429. diff --git a/packages/cli/src/commands/connector/utils.ts b/packages/cli/src/commands/connector/utils.ts index 06c36cbc7..1e0d8cb85 100644 --- a/packages/cli/src/commands/connector/utils.ts +++ b/packages/cli/src/commands/connector/utils.ts @@ -168,26 +168,25 @@ export const fetchOfficialConnectorList = async (includingCloudConnectors = fals const packages: PackageMeta[] = []; + const excludeList = ['mock', 'kit', ...conditionalArray(!includingCloudConnectors && 'logto')]; + + // The API called by `fetchList` performs a 'fuzzy' search based on the `text` parameter, which will yield many irrelevant results. We need to filter out these irrelevant results (using `name.startsWith(officialConnectorPrefix)` for filtering). // Disable lint rules for business need // eslint-disable-next-line @silverhand/fp/no-let, @silverhand/fp/no-mutation for (let page = 0; ; ++page) { // eslint-disable-next-line no-await-in-loop - const { objects } = await fetchList(page * 20, 20); + const { objects: rawObjects } = await fetchList(page * 20, 20); - const excludeList = ['mock', 'kit', ...conditionalArray(!includingCloudConnectors && 'logto')]; + const objects = rawObjects.filter( + ({ package: { name, scope } }) => + name.startsWith(officialConnectorPrefix) && + excludeList.every( + (excluded) => !name.slice(officialConnectorPrefix.length).startsWith(excluded) + ) + ); // eslint-disable-next-line @silverhand/fp/no-mutating-methods - packages.push( - ...objects - .filter( - ({ package: { name, scope } }) => - scope === 'logto' && - excludeList.every( - (excluded) => !name.slice(officialConnectorPrefix.length).startsWith(excluded) - ) - ) - .map(({ package: data }) => data) - ); + packages.push(...objects.map(({ package: data }) => data)); if (objects.length < 20) { break;