Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass auth to custom registry #77

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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