Skip to content

Commit

Permalink
fix: pass auth to custom registry (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 authored Feb 16, 2023
1 parent af23a7d commit 28ef2ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/giget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function downloadTemplate (input: string, options: DownloadTemplate
auth: process.env.GIGET_AUTH
}, options);

const registry = options.registry !== false ? registryProvider(options.registry) : undefined;
const registry = options.registry !== false ? registryProvider(options.registry, { auth: options.auth }) : undefined;
let providerName: string = options.provider || (registryProvider ? "registry" : "github");
let source: string = input;
const sourceProvierMatch = input.match(sourceProtoRe);
Expand All @@ -48,7 +48,7 @@ export async function downloadTemplate (input: string, options: DownloadTemplate
throw new Error(`Failed to download template from ${providerName}: ${error.message}`);
});

// Sanetize name and defaultDir
// Sanitize name and defaultDir
template.name = (template.name || "template").replace(/[^\da-z-]/gi, "-");
template.defaultDir = (template.defaultDir || template.name).replace(/[^\da-z-]/gi, "-");

Expand All @@ -71,11 +71,16 @@ export async function downloadTemplate (input: string, options: DownloadTemplate
if (!options.offline) {
await mkdir(dirname(tarPath), { recursive: true });
const s = Date.now();
await download(template.tar, tarPath, { headers: template.headers }).catch((error) => {
await download(template.tar, tarPath, {
headers: {
authorization: options.auth ? `Bearer ${options.auth}` : undefined,
...template.headers,
}
}).catch((error) => {
if (!existsSync(tarPath)) {
throw error;
}
// Accept netwrok errors if we have a cached version
// Accept network errors if we have a cached version
debug("Download error. Using cached version:", error);
options.offline = true;
});
Expand Down
9 changes: 7 additions & 2 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { debug, sendFetch } from "./_utils";
// const DEFAULT_REGISTRY = 'https://cdn.jsdelivr.net/gh/unjs/giget/templates'
const DEFAULT_REGISTRY = "https://raw.githubusercontent.com/unjs/giget/main/templates";

export const registryProvider = (registryEndpoint: string = DEFAULT_REGISTRY) => {
export const registryProvider = (registryEndpoint: string = DEFAULT_REGISTRY, options?: { auth?: string }) => {
options = options || {};
return <TemplateProvider>(async (input) => {
const start = Date.now();
const registryURL = `${registryEndpoint}/${input}.json`;

const result = await sendFetch(registryURL);
const result = await sendFetch(registryURL, {
headers: {
Authorization: options.auth ? `Bearer ${options.auth}` : undefined,
}
});
if (result.status >= 400) {
throw new Error(`Failed to download ${input} template info from ${registryURL}: ${result.status} ${result.statusText}`);
}
Expand Down

0 comments on commit 28ef2ef

Please sign in to comment.