From 7242dca042159ac7710e09baa380341e3e7a76c5 Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Wed, 8 Jun 2022 10:59:47 -1000 Subject: [PATCH] fix: resolve issue where `getTags()` returned an empty array for secured repositories --- src/client.ts | 8 +++++++- test/client-getTags.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 0da6300d..578d5c47 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1150,7 +1150,13 @@ export class Client { try { const tagsForm = await this.getCachedRepositoryForm("tags", params); - return await this.fetch(tagsForm.action); + const url = new URL(tagsForm.action); + + if (this.accessToken) { + url.searchParams.set("access_token", this.accessToken); + } + + return await this.fetch(url.toString(), params); } catch { const repository = await this.getRepository(params); diff --git a/test/client-getTags.test.ts b/test/client-getTags.test.ts index 0818bccd..c05866be 100644 --- a/test/client-getTags.test.ts +++ b/test/client-getTags.test.ts @@ -48,6 +48,36 @@ test("uses form endpoint if available", async (t) => { t.deepEqual(res, tagsResponse); }); +test("sends access token if form endpoint is used", async (t) => { + const tagsEndpoint = "https://example.com/tags-form-endpoint"; + const tagsResponse = ["foo", "bar"]; + const accessToken = "accessToken"; + + const repositoryResponse = createRepositoryResponse({ + forms: { + tags: { + method: "GET", + action: tagsEndpoint, + enctype: "", + fields: {}, + }, + }, + }); + server.use( + createMockRepositoryHandler(t, repositoryResponse), + msw.rest.get(tagsEndpoint, (req, res, ctx) => { + if (req.url.searchParams.get("access_token") === accessToken) { + return res(ctx.json(tagsResponse)); + } + }), + ); + + const client = createTestClient(t, { accessToken }); + const res = await client.getTags(); + + t.deepEqual(res, tagsResponse); +}); + test("is abortable with an AbortController", async (t) => { const repositoryResponse = createRepositoryResponse();