Skip to content

Commit

Permalink
Drop got dependency (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Dec 13, 2023
1 parent ca86555 commit 935374b
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 212 deletions.
49 changes: 29 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import got from 'got';

const rootURI = 'https://www.googleapis.com';
const refreshTokenURI = 'https://www.googleapis.com/oauth2/v4/token';
export const refreshTokenURI = 'https://www.googleapis.com/oauth2/v4/token';
const uploadExistingURI = id =>
`${rootURI}/upload/chromewebstore/v1.1/items/${id}`;
const publishURI = (id, target) =>
Expand Down Expand Up @@ -33,32 +31,35 @@ class APIClient {

const { extensionId } = this;

return got
.put(uploadExistingURI(extensionId), {
headers: this._headers(await token),
body: readStream,
})
.json();
const request = await fetch(uploadExistingURI(extensionId), {
method: 'PUT',
headers: this._headers(await token),
body: readStream,
});

return request.json();
}

async publish(target = 'default', token = this.fetchToken()) {
const { extensionId } = this;

return got
.post(publishURI(extensionId, target), {
headers: this._headers(await token),
})
.json();
const request = await fetch(publishURI(extensionId, target), {
method: 'POST',
headers: this._headers(await token),
});

return request.json();
}

async get(projection = 'DRAFT', token = this.fetchToken()) {
const { extensionId } = this;

return got
.get(getURI(extensionId, projection), {
headers: this._headers(await token),
})
.json();
const request = await fetch(getURI(extensionId, projection), {
method: 'GET',
headers: this._headers(await token),
});

return request.json();
}

async fetchToken() {
Expand All @@ -73,7 +74,15 @@ class APIClient {
json.client_secret = clientSecret;
}

const response = await got.post(refreshTokenURI, { json }).json();
const request = await fetch(refreshTokenURI, {
method: 'POST',
body: JSON.stringify(json),
headers: {
'Content-Type': 'application/json',
},
});

const response = await request.json();

return response.access_token;
}
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@
},
"space": 4
},
"dependencies": {
"got": "^14.0.0"
},
"devDependencies": {
"ava": "^6.0.1",
"sinon": "^17.0.1",
"fetch-mock": "^9.11.0",
"xo": "^0.56.0"
},
"engines": {
Expand Down
26 changes: 7 additions & 19 deletions test/fetch-token.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
import test from 'ava';
import got from 'got';
import sinon from 'sinon';
import fetchMock from 'fetch-mock';
import { refreshTokenURI } from '../index.js';
import getClient from './helpers/get-client.js';

test.beforeEach('Setup Sinon Sandbox', t => {
test.beforeEach(t => {
t.context = {
sandbox: sinon.createSandbox(),
client: getClient(),
};
});

test.afterEach('Reset Sinon Sandbox', t => {
t.context.sandbox.restore();
});

// TODO: Find a better way of handling stubbing, to eliminate the need
// to run tests serially - https://github.com/avajs/ava/issues/295#issuecomment-161123805

test.serial('Only returns token from response body', async t => {
t.plan(1);

const { client, sandbox } = t.context;
test('Only returns token from response body', async t => {
const { client } = t.context;
const accessToken = 'access-token';

sandbox.stub(got, 'post').returns({
json: sandbox.stub().resolves({
access_token: accessToken,
}),
fetchMock.post(refreshTokenURI, {
access_token: accessToken,
});

t.is(await client.fetchToken(), accessToken);
Expand Down
83 changes: 26 additions & 57 deletions test/get.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,54 @@
import test from 'ava';
import got from 'got';
import sinon from 'sinon';
import fetchMock from 'fetch-mock';
import getClient from './helpers/get-client.js';

test.beforeEach('Setup Sinon Sandbox', t => {
test.beforeEach(t => {
fetchMock.reset();
t.context = {
sandbox: sinon.createSandbox(),
client: getClient(),
};
});

test.afterEach('Reset Sinon Sandbox', t => {
t.context.sandbox.restore();
});

// TODO: Find a better way of handling stubbing, to eliminate the need
// to run tests serially - https://github.com/avajs/ava/issues/295#issuecomment-161123805

test.serial('Get uses default projection when not provided', async t => {
t.plan(1);

const { client, sandbox } = t.context;
const defaultProjection = 'DRAFT';

sandbox.stub(got, 'get').callsFake(uri => {
t.is(new URL(uri).searchParams.get('projection'), defaultProjection);

return {
json: sandbox.stub().resolves({}),
};
});
test('Get uses default projection when not provided', async t => {
const { client } = t.context;

const mock = fetchMock.getOnce('begin:https://www.googleapis.com', {});
await client.get(undefined, 'token');
});

test.serial('Get does not fetch token when provided', async t => {
const { client, sandbox } = t.context;

sandbox.stub(got, 'get').callsFake(uri => {
if (uri === 'https://accounts.google.com/o/oauth2/token') {
return t.fail('Token should not have been fetched');
}
t.is(
mock.lastUrl(),
'https://www.googleapis.com/chromewebstore/v1.1/items/foo?projection=DRAFT',
);
});

return {
json: sandbox.stub().resolves({}),
};
});
test('Get does not fetch token when provided', async t => {
t.plan(0);
const { client } = t.context;

fetchMock.getOnce('begin:https://www.googleapis.com/chromewebstore/v1.1/items/', {});
await client.get(undefined, 'token');
t.pass('Did not fetch token');
});

test.serial('Get uses token for auth', async t => {
t.plan(1);
test('Get uses token for auth', async t => {
t.plan(0);

const { client, sandbox } = t.context;
const { client } = t.context;
const token = 'token';

sandbox.stub(got, 'get').callsFake((uri, { headers }) => {
t.is(headers.Authorization, `Bearer ${token}`);
return {
json: sandbox.stub().resolves({}),
};
});
fetchMock.getOnce({
url: 'begin:https://www.googleapis.com/',
headers: { Authorization: `Bearer ${token}` },
}, {});

await client.get(undefined, token);
});

test.serial('Get uses provided extension ID', async t => {
t.plan(1);

const { client, sandbox } = t.context;
test('Get uses provided extension ID', async t => {
const { client } = t.context;
const { extensionId } = client;

sandbox.stub(got, 'get').callsFake(uri => {
t.true(uri.includes(`/items/${extensionId}`));

return {
json: sandbox.stub().resolves({}),
};
});
fetchMock.getOnce(`path:/chromewebstore/v1.1/items/${extensionId}`, {});

await client.get(undefined, 'token');
t.pass();
});
90 changes: 30 additions & 60 deletions test/publish.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,72 @@
import test from 'ava';
import got from 'got';
import sinon from 'sinon';
import fetchMock from 'fetch-mock';
import getClient from './helpers/get-client.js';

test.beforeEach('Setup Sinon Sandbox', t => {
test.beforeEach(t => {
fetchMock.reset();
t.context = {
sandbox: sinon.createSandbox(),
client: getClient(),
};
});

test.afterEach('Reset Sinon Sandbox', t => {
t.context.sandbox.restore();
});

// TODO: Find a better way of handling stubbing, to eliminate the need
// to run tests serially - https://github.com/avajs/ava/issues/295#issuecomment-161123805

test.serial('Publish uses default target when not provided', async t => {
t.plan(1);

const { client, sandbox } = t.context;
const defaultTarget = 'default';

sandbox.stub(got, 'post').callsFake(uri => {
t.is(new URL(uri).searchParams.get('publishTarget'), defaultTarget);

return {
json: sandbox.stub().resolves({}),
};
});
t.plan(0);
const { client } = t.context;
fetchMock.postOnce('https://www.googleapis.com/chromewebstore/v1.1/items/foo/publish?publishTarget=default', {});

await client.publish(undefined, 'token');
});

test.serial('Publish uses target when provided', async t => {
t.plan(1);

const { client, sandbox } = t.context;
t.plan(0);
const { client } = t.context;
const target = 'trustedTesters';

sandbox.stub(got, 'post').callsFake(uri => {
t.is(new URL(uri).searchParams.get('publishTarget'), target);

return {
json: sandbox.stub().resolves({}),
};
});
fetchMock.postOnce(`https://www.googleapis.com/chromewebstore/v1.1/items/foo/publish?publishTarget=${target}`, {});

await client.publish(target, 'token');
});

test.serial('Publish does not fetch token when provided', async t => {
const { client, sandbox } = t.context;
t.plan(0);
const { client } = t.context;

sandbox.stub(got, 'post').callsFake(uri => {
if (uri === 'https://accounts.google.com/o/oauth2/token') {
return t.fail('Token should not have been fetched');
}

return {
json: sandbox.stub().resolves({}),
};
});
fetchMock.postOnce('https://www.googleapis.com/chromewebstore/v1.1/items/foo/publish?publishTarget=default', {});

await client.publish(undefined, 'token');
t.pass('Did not fetch token');
});

test.serial('Publish uses token for auth', async t => {
t.plan(1);
t.plan(0);

const { client, sandbox } = t.context;
const { client } = t.context;
const token = 'token';

sandbox.stub(got, 'post').callsFake((uri, { headers }) => {
t.is(headers.Authorization, `Bearer ${token}`);
return {
json: sandbox.stub().resolves({}),
};
});
fetchMock.postOnce({
url: 'https://www.googleapis.com/chromewebstore/v1.1/items/foo/publish?publishTarget=default',
headers: {
Authorization: `Bearer ${token}`,
},
}, {});

await client.publish(undefined, token);
});

test.serial('Uses provided extension ID', async t => {
t.plan(1);
t.plan(0);

const { client, sandbox } = t.context;
const { client } = t.context;
const { extensionId } = client;

sandbox.stub(got, 'post').callsFake(uri => {
t.true(uri.includes(`/items/${extensionId}`));
// Sandbox.stub(got, 'post').callsFake(uri => {
// t.true(uri.includes(`/items/${extensionId}`));

// return {
// json: sandbox.stub().resolves({}),
// };
// });

return {
json: sandbox.stub().resolves({}),
};
});
fetchMock.postOnce(`https://www.googleapis.com/chromewebstore/v1.1/items/${extensionId}/publish?publishTarget=default`, {});

await client.publish(undefined, 'token');
});
Expand Down
Loading

0 comments on commit 935374b

Please sign in to comment.