-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wrangler] teach wrangler to search docs (#3004)
- Loading branch information
Showing
10 changed files
with
106 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feat: teach `wrangler docs` to use algolia search index | ||
|
||
This PR lets you search Cloudflare's entire docs via `wrangler docs [search term here]`. | ||
|
||
By default, if the search fails to find what you're looking for, you'll get an error like this: | ||
|
||
``` | ||
✘ [ERROR] Could not find docs for: <search term goes here>. Please try again with another search term. | ||
``` | ||
|
||
If you provide the `--yes` or `-y` flag, wrangler will open the docs to https://developers.cloudflare.com/workers/wrangler/commands/, even if the search fails. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import assert from "node:assert"; | ||
import { fetch } from "undici"; | ||
import { logger } from "../logger"; | ||
|
||
// The ALGOLIA_APP_ID and ALGOLIA_PUBLIC_KEY are provided at esbuild time as a `define` for production and beta releases. | ||
// Otherwise it is left undefined, which disables search. | ||
declare const ALGOLIA_APP_ID: string; | ||
declare const ALGOLIA_PUBLIC_KEY: string; | ||
|
||
export async function runSearch(searchTerm: string) { | ||
const id = ALGOLIA_APP_ID; | ||
const index = "developers-cloudflare2"; | ||
const key = ALGOLIA_PUBLIC_KEY; | ||
const params = new URLSearchParams({ | ||
query: searchTerm, | ||
hitsPerPage: "1", | ||
getRankingInfo: "0", | ||
}); | ||
|
||
assert(id, "Missing Algolia App ID"); | ||
assert(key, "Missing Algolia Key"); | ||
|
||
const searchResp = await fetch( | ||
`https://${id}-dsn.algolia.net/1/indexes/${index}/query`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ | ||
params: params.toString(), | ||
}), | ||
headers: { | ||
"X-Algolia-API-Key": key, | ||
"X-Algolia-Application-Id": id, | ||
}, | ||
} | ||
); | ||
if (!searchResp.ok) { | ||
logger.error(`Could not search the docs. Please try again later.`); | ||
return; | ||
} | ||
const searchData = (await searchResp.json()) as { hits: { url: string }[] }; | ||
logger.debug("searchData: ", searchData); | ||
if (searchData.hits[0]) { | ||
return searchData.hits[0].url; | ||
} else { | ||
logger.error( | ||
`Could not find docs for: ${searchTerm}. Please try again with another search term.` | ||
); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters