|
| 1 | +# fetchRawFile |
| 2 | + |
| 3 | +Fetches the raw content of a file from a GitHub repository via `raw.githubusercontent.com`. |
| 4 | + |
| 5 | +```ts |
| 6 | +import { fetchRawFile } from "@openally/github.sdk"; |
| 7 | + |
| 8 | +// Fetch file as plain text (default) |
| 9 | +const content = await fetchRawFile("nodejs/node", "README.md"); |
| 10 | + |
| 11 | +// Fetch and parse as JSON |
| 12 | +const pkg = await fetchRawFile<{ version: string }>("nodejs/node", "package.json", { |
| 13 | + parser: "json" |
| 14 | +}); |
| 15 | + |
| 16 | +// Fetch and parse with a custom parser |
| 17 | +const lines = await fetchRawFile("nodejs/node", ".gitignore", { |
| 18 | + parser: (content) => content.split("\n").filter(Boolean) |
| 19 | +}); |
| 20 | + |
| 21 | +// Fetch from a specific branch or tag |
| 22 | +const content = await fetchRawFile("nodejs/node", "README.md", { |
| 23 | + ref: "v20.0.0" |
| 24 | +}); |
| 25 | + |
| 26 | +// Fetch a private file with a token |
| 27 | +const content = await fetchRawFile("myorg/private-repo", "config.json", { |
| 28 | + token: process.env.GITHUB_TOKEN, |
| 29 | + parser: "json" |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +## Signature |
| 34 | + |
| 35 | +```ts |
| 36 | +function fetchRawFile( |
| 37 | + repository: `${string}/${string}`, |
| 38 | + filePath: string, |
| 39 | + options?: FetchRawFileOptions |
| 40 | +): Promise<string>; |
| 41 | + |
| 42 | +function fetchRawFile<T>( |
| 43 | + repository: `${string}/${string}`, |
| 44 | + filePath: string, |
| 45 | + options: FetchRawFileOptions & { parser: "json" } |
| 46 | +): Promise<T>; |
| 47 | + |
| 48 | +function fetchRawFile<T>( |
| 49 | + repository: `${string}/${string}`, |
| 50 | + filePath: string, |
| 51 | + options: FetchRawFileOptions & { parser: (content: string) => T } |
| 52 | +): Promise<T>; |
| 53 | +``` |
| 54 | + |
| 55 | +## Parameters |
| 56 | + |
| 57 | +### `repository` |
| 58 | + |
| 59 | +Type: `` `${string}/${string}` `` |
| 60 | + |
| 61 | +The repository in `owner/repo` format (e.g. `"nodejs/node"`). |
| 62 | + |
| 63 | +### `filePath` |
| 64 | + |
| 65 | +Type: `string` |
| 66 | + |
| 67 | +Path to the file within the repository (e.g. `"src/index.ts"` or `"README.md"`). |
| 68 | + |
| 69 | +### `options` |
| 70 | + |
| 71 | +```ts |
| 72 | +interface FetchRawFileOptions extends RequestConfig { |
| 73 | + /** |
| 74 | + * Branch, tag, or commit SHA. |
| 75 | + * @default "HEAD" |
| 76 | + */ |
| 77 | + ref?: string; |
| 78 | +} |
| 79 | + |
| 80 | +interface RequestConfig { |
| 81 | + /** |
| 82 | + * A personal access token is required to access private resources, |
| 83 | + * and to increase the rate limit for unauthenticated requests. |
| 84 | + */ |
| 85 | + token?: string; |
| 86 | + /** |
| 87 | + * @default "@openally/github.sdk/1.0.0" |
| 88 | + * @see https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api?apiVersion=2022-11-28#user-agent |
| 89 | + */ |
| 90 | + userAgent?: string; |
| 91 | +} |
| 92 | + |
| 93 | +``` |
| 94 | + |
| 95 | +## Return value |
| 96 | + |
| 97 | +- `Promise<string>` when no `parser` is provided. |
| 98 | +- `Promise<T>` when `parser: "json"` or a custom parser function is provided. |
| 99 | + |
| 100 | +## Errors |
| 101 | + |
| 102 | +Throws an `Error` if the HTTP response is not `ok` (e.g. 404 for a missing file, 401 for an unauthorized request): |
| 103 | + |
| 104 | +``` |
| 105 | +Failed to fetch raw file 'README.md' from nodejs/node@HEAD: HTTP 404 |
| 106 | +``` |
0 commit comments