Skip to content

Commit 2c97e89

Browse files
authored
feat: auth({ type: "get" }) (#30)
1 parent 4065c69 commit 2c97e89

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ Without setting `type` auth will return the current authentication object, or ex
590590

591591
Possible values for `type` are
592592

593+
- `"get"`: returns the token from internal state and creates it if none was created yet
593594
- `"check"`: sends request to verify the validity of the current token
594595
- `"reset"`: invalidates current token and replaces it with a new one
595596
- `"refresh"`: GitHub Apps only, and only if expiring user tokens are enabled.

src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,15 @@ export type WebFlowState = {
165165
};
166166

167167
export type OAuthAppAuthOptions = {
168-
type?: "check" | "reset" | "delete" | "deleteAuthorization";
168+
type?: "get" | "check" | "reset" | "delete" | "deleteAuthorization";
169169
};
170170

171171
export type GitHubAppAuthOptions = {
172-
type?: "check" | "reset" | "refresh" | "delete" | "deleteAuthorization";
172+
type?:
173+
| "get"
174+
| "check"
175+
| "reset"
176+
| "refresh"
177+
| "delete"
178+
| "deleteAuthorization";
173179
};

test/standalone.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,62 @@ describe("refreshing tokens", () => {
549549
});
550550
});
551551

552+
describe("auth({ type: 'get' })", () => {
553+
it("is valid", async () => {
554+
const mock = fetchMock.sandbox().postOnce(
555+
"https://api.github.com/applications/1234567890abcdef1234/token",
556+
{
557+
scopes: [],
558+
},
559+
{
560+
headers: {
561+
accept: "application/vnd.github.v3+json",
562+
"user-agent": "test",
563+
authorization: "basic MTIzNDU2Nzg5MGFiY2RlZjEyMzQ6c2VjcmV0",
564+
"content-type": "application/json; charset=utf-8",
565+
},
566+
body: { access_token: "token123" },
567+
}
568+
);
569+
570+
const auth = createOAuthUserAuth({
571+
clientType: "oauth-app",
572+
clientId: "1234567890abcdef1234",
573+
clientSecret: "secret",
574+
token: "token123",
575+
scopes: [],
576+
577+
// pass request mock for testing
578+
request: request.defaults({
579+
headers: {
580+
"user-agent": "test",
581+
},
582+
request: {
583+
fetch: mock,
584+
},
585+
}),
586+
});
587+
588+
const authentication1 = await auth({
589+
type: "get",
590+
});
591+
const authentication2 = await auth({
592+
type: "get",
593+
});
594+
595+
expect(authentication1).toEqual({
596+
type: "token",
597+
tokenType: "oauth",
598+
clientType: "oauth-app",
599+
clientId: "1234567890abcdef1234",
600+
clientSecret: "secret",
601+
token: "token123",
602+
scopes: [],
603+
});
604+
expect(authentication1).toEqual(authentication2);
605+
});
606+
});
607+
552608
describe("auth({ type: 'check' })", () => {
553609
it("is valid", async () => {
554610
const mock = fetchMock.sandbox().postOnce(

0 commit comments

Comments
 (0)