Skip to content

Commit

Permalink
Merge pull request #1 from bkspace/add-get-and-delete
Browse files Browse the repository at this point in the history
Add get and delete
  • Loading branch information
bkspace authored Jun 27, 2020
2 parents e467c85 + 810fe8f commit 608b115
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 28 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,26 @@ await injectScript(
)
```

This function returns a response. It is left to the consumer to handle any possible errors. I expect this library will handle both fetch and a gql client in the near future.
```
await getScripts(
fetch,
accessToken,
shop,
)
```

```
await deleteScript(
fetch,
accessToken,
shop,
'1234',
)
```

These function return a fetch response. It is left to the consumer to handle any possible errors. I expect this library will handle both fetch and a gql client in the near future.

Note - you can add the same script multiple times, so injecting a script should be done once, or validate that the script being injected is different and/or versioned.

## Development
```yarn build```
Expand Down
41 changes: 39 additions & 2 deletions dist/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.injectScript = void 0;
exports.removeScript = exports.getScripts = exports.injectScript = void 0;
exports.injectScript = function (fetch, accessToken, shop, scriptUrl) { return __awaiter(void 0, void 0, void 0, function () {
var createScriptTagUrl, shopRequestHeaders, scriptTagBody;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
createScriptTagUrl = 'https://' + shop + '/admin/api/2020-04/script_tags.json';
createScriptTagUrl = "https://" + shop + "/admin/api/2020-04/script_tags.json";
shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
Accept: 'application/json',
Expand All @@ -63,3 +63,40 @@ exports.injectScript = function (fetch, accessToken, shop, scriptUrl) { return _
}
});
}); };
exports.getScripts = function (fetch, accessToken, shop) { return __awaiter(void 0, void 0, void 0, function () {
var createScriptTagUrl, shopRequestHeaders;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
createScriptTagUrl = "https://" + shop + "/admin/api/2020-04/script_tags.json";
shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
Accept: 'application/json',
'Content-Type': 'application/json',
};
return [4 /*yield*/, fetch(createScriptTagUrl, {
headers: shopRequestHeaders,
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };
exports.removeScript = function (fetch, accessToken, shop, scriptId) { return __awaiter(void 0, void 0, void 0, function () {
var createScriptTagUrl, shopRequestHeaders;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
createScriptTagUrl = "https://" + shop + "/admin/api/2020-04/script_tags/" + scriptId + ".json";
shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
Accept: 'application/json',
'Content-Type': 'application/json',
};
return [4 /*yield*/, fetch(createScriptTagUrl, {
method: 'DELETE',
headers: shopRequestHeaders,
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };
2 changes: 2 additions & 0 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export declare const injectScript: (fetch: (url: string, opts: any) => Promise<Response>, accessToken: string, shop: string, scriptUrl: string) => Promise<Response>;
export declare const getScripts: (fetch: (url: string, opts: any) => Promise<Response>, accessToken: string, shop: string) => Promise<Response>;
export declare const removeScript: (fetch: (url: string, opts: any) => Promise<Response>, accessToken: string, shop: string, scriptId: string) => Promise<Response>;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shopify-script-tags",
"version": "0.0.1",
"version": "0.0.2",
"main": "./dist/lib/index.js",
"typings": "./dist/types/index.d.ts",
"license": "MIT",
Expand Down
88 changes: 66 additions & 22 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,70 @@
import { injectScript } from '../'
import { injectScript, getScripts, removeScript } from '../'

describe('injectScript', () => {
it('makes a POST request to add a script tag', async () => {
const mockFetch = jest.fn().mockResolvedValue('foo')
const result = await injectScript(
mockFetch,
'access_token',
'my_shop',
'script_url',
)
describe('scripts', () => {
describe('injectScript', () => {
it('makes a POST request to add a script tag', async () => {
const mockFetch = jest.fn().mockResolvedValue('foo')
const result = await injectScript(
mockFetch,
'access_token',
'my_shop',
'script_url',
)
const expectedAPIUri =
'https://my_shop/admin/api/2020-04/script_tags.json'
const expectedOptions = {
body: '{"script_tag":{"event":"onload","src":"script_url"}}',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Shopify-Access-Token': 'access_token',
},
method: 'POST',
}
expect(result).toEqual('foo')
expect(mockFetch).toHaveBeenCalledWith(expectedAPIUri, expectedOptions)
})
})

describe('getScripts', () => {
it('makes a GET request to retrieve all active script tags', async () => {
const mockFetch = jest.fn().mockResolvedValue('foo')
const result = await getScripts(mockFetch, 'access_token', 'my_shop')
const expectedAPIUri =
'https://my_shop/admin/api/2020-04/script_tags.json'
const expectedOptions = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Shopify-Access-Token': 'access_token',
},
}
expect(result).toEqual('foo')
expect(mockFetch).toHaveBeenCalledWith(expectedAPIUri, expectedOptions)
})
})

const expectedAPIUri = 'https://my_shop/admin/api/2020-04/script_tags.json'
const expectedOptions = {
body: '{"script_tag":{"event":"onload","src":"script_url"}}',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Shopify-Access-Token': 'access_token',
},
method: 'POST',
}
expect(result).toEqual('foo')
expect(mockFetch).toHaveBeenCalledWith(expectedAPIUri, expectedOptions)
describe('removeScript', () => {
it('makes a DELETE request to remove a specific script', async () => {
const mockFetch = jest.fn().mockResolvedValue('foo')
const result = await removeScript(
mockFetch,
'access_token',
'my_shop',
'foobar',
)
const expectedAPIUri =
'https://my_shop/admin/api/2020-04/script_tags/foobar.json'
const expectedOptions = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Shopify-Access-Token': 'access_token',
},
method: 'DELETE',
}
expect(result).toEqual('foo')
expect(mockFetch).toHaveBeenCalledWith(expectedAPIUri, expectedOptions)
})
})
})
39 changes: 37 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ export const injectScript = async (
shop: string,
scriptUrl: string,
): Promise<Response> => {
const createScriptTagUrl =
'https://' + shop + '/admin/api/2020-04/script_tags.json'
const createScriptTagUrl = `https://${shop}/admin/api/2020-04/script_tags.json`
const shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
Accept: 'application/json',
Expand All @@ -25,3 +24,39 @@ export const injectScript = async (
body: JSON.stringify(scriptTagBody),
})
}

export const getScripts = async (
fetch: (url: string, opts: any) => Promise<Response>,
accessToken: string,
shop: string,
) => {
const createScriptTagUrl = `https://${shop}/admin/api/2020-04/script_tags.json`
const shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
Accept: 'application/json',
'Content-Type': 'application/json',
}

return await fetch(createScriptTagUrl, {
headers: shopRequestHeaders,
})
}

export const removeScript = async (
fetch: (url: string, opts: any) => Promise<Response>,
accessToken: string,
shop: string,
scriptId: string,
) => {
const createScriptTagUrl = `https://${shop}/admin/api/2020-04/script_tags/${scriptId}.json`
const shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
Accept: 'application/json',
'Content-Type': 'application/json',
}

return await fetch(createScriptTagUrl, {
method: 'DELETE',
headers: shopRequestHeaders,
})
}

0 comments on commit 608b115

Please sign in to comment.