-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More helpers for #945 Introduces `checkRepoAccess` to check if user has read access to repo
- Loading branch information
1 parent
3f99361
commit 186ab73
Showing
7 changed files
with
82 additions
and
26 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
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,34 @@ | ||
import { assert, describe, expect, it } from "vitest"; | ||
import { checkRepoAccess } from "./check-repo-access"; | ||
import { HubApiError } from "../error"; | ||
import { TEST_ACCESS_TOKEN, TEST_HUB_URL } from "../test/consts"; | ||
|
||
describe("checkRepoAccess", () => { | ||
it("should throw 401 when accessing unexisting repo unauthenticated", async () => { | ||
try { | ||
await checkRepoAccess({ repo: { name: "i--d/dont", type: "model" } }); | ||
assert(false, "should have thrown"); | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(HubApiError); | ||
expect((err as HubApiError).statusCode).toBe(401); | ||
} | ||
}); | ||
|
||
it("should throw 404 when accessing unexisting repo authenticated", async () => { | ||
try { | ||
await checkRepoAccess({ | ||
repo: { name: "i--d/dont", type: "model" }, | ||
hubUrl: TEST_HUB_URL, | ||
accessToken: TEST_ACCESS_TOKEN, | ||
}); | ||
assert(false, "should have thrown"); | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(HubApiError); | ||
expect((err as HubApiError).statusCode).toBe(404); | ||
} | ||
}); | ||
|
||
it("should not throw when accessing public repo", async () => { | ||
await checkRepoAccess({ repo: { name: "openai-community/gpt2", type: "model" } }); | ||
}); | ||
}); |
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,32 @@ | ||
import { HUB_URL } from "../consts"; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import { createApiError, type HubApiError } from "../error"; | ||
import type { CredentialsParams, RepoDesignation } from "../types/public"; | ||
import { checkCredentials } from "../utils/checkCredentials"; | ||
import { toRepoId } from "../utils/toRepoId"; | ||
|
||
/** | ||
* Check if we have read access to a repository. | ||
* | ||
* Throw a {@link HubApiError} error if we don't have access. HubApiError.statusCode will be 401, 403 or 404. | ||
*/ | ||
export async function checkRepoAccess( | ||
params: { | ||
repo: RepoDesignation; | ||
hubUrl?: string; | ||
fetch?: typeof fetch; | ||
} & Partial<CredentialsParams> | ||
): Promise<void> { | ||
const accessToken = params && checkCredentials(params); | ||
const repoId = toRepoId(params.repo); | ||
|
||
const response = await (params.fetch || fetch)(`${params?.hubUrl || HUB_URL}/api/${repoId.type}s/${repoId.name}`, { | ||
headers: { | ||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}), | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw await createApiError(response); | ||
} | ||
} |
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