Skip to content

Commit 9b38425

Browse files
Merge pull request #169 from Shopify/sample-apps-javascript
Adding JavaScript functions to sample apps
2 parents a253d20 + 7869cc4 commit 9b38425

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+29351
-9883
lines changed

.graphqlrc.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ function getProjects(path) {
2626

2727
const projects = {
2828
...getProjects("sample-apps/discounts/extensions"),
29+
...getProjects("sample-apps/payment-customizations/extensions"),
30+
...getProjects("sample-apps/delivery-customizations/extensions"),
2931
...getProjects("checkout/rust/delivery-customization"),
3032
...getProjects("checkout/rust/payment-customization"),
3133
...getProjects("checkout/javascript/delivery-customization"),

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ members = [
33
"*/rust/*/*",
44
"sample-apps/*/extensions/*",
55
]
6+
exclude = [
7+
# Excludes do not support globbing -- https://github.com/rust-lang/cargo/issues/11405
8+
"sample-apps/delivery-customizations/extensions/delivery-customization-js",
9+
"sample-apps/discounts/extensions/product-discount-js",
10+
"sample-apps/payment-customizations/extensions/payment-customization-js/",
11+
]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"private": true,
2121
"workspaces": [
22-
"*/javascript/**"
22+
"*/javascript/**",
23+
"sample-apps/*/extensions/*-js"
2324
]
2425
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
generated
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "delivery-customization",
3+
"version": "0.0.1",
4+
"license": "UNLICENSED",
5+
"scripts": {
6+
"shopify": "npm exec -- shopify",
7+
"typegen": "npm exec -- shopify app function typegen",
8+
"build": "npm exec -- shopify app function build",
9+
"preview": "npm exec -- shopify app function run",
10+
"test": "vitest"
11+
},
12+
"codegen": {
13+
"schema": "schema.graphql",
14+
"documents": "input.graphql",
15+
"generates": {
16+
"./generated/api.ts": {
17+
"plugins": [
18+
"typescript",
19+
"typescript-operations"
20+
]
21+
}
22+
}
23+
},
24+
"devDependencies": {
25+
"vitest": "^0.29.2"
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name = "delivery-customization-js"
2+
type = "delivery_customization"
3+
api_version = "2023-04"
4+
5+
[build]
6+
command = ""
7+
path = "dist/function.wasm"
8+
9+
[ui.paths]
10+
create = "/delivery-customization/:functionId/new"
11+
details = "/delivery-customization/:functionId/:id"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @ts-check
2+
3+
/**
4+
* @typedef {import("../generated/api").InputQuery} InputQuery
5+
* @typedef {import("../generated/api").FunctionResult} FunctionResult
6+
* @typedef {import("../generated/api").Operation} Operation
7+
*/
8+
9+
/**
10+
* @type {FunctionResult}
11+
*/
12+
const NO_CHANGES = {
13+
operations: [],
14+
};
15+
16+
export default /**
17+
* @param {InputQuery} input
18+
* @returns {FunctionResult}
19+
*/
20+
(input) => {
21+
/**
22+
* @type {{
23+
* stateProvinceCode: string
24+
* message: number
25+
* }}
26+
*/
27+
const configuration = JSON.parse(
28+
input?.deliveryCustomization?.metafield?.value ?? "{}"
29+
);
30+
if (!configuration.stateProvinceCode || !configuration.message) {
31+
return NO_CHANGES;
32+
}
33+
34+
let toRename = input.cart.deliveryGroups
35+
.filter(group => group.deliveryAddress?.provinceCode &&
36+
group.deliveryAddress.provinceCode == configuration.stateProvinceCode)
37+
.flatMap(group => group.deliveryOptions)
38+
.map(option => /** @type {Operation} */({
39+
rename: {
40+
deliveryOptionHandle: option.handle,
41+
title: option.title ? `${option.title} - ${configuration.message}` : configuration.message
42+
}
43+
}));
44+
45+
return {
46+
operations: toRename
47+
};
48+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { describe, it, expect } from 'vitest';
2+
import deliveryCustomization from './index';
3+
4+
/**
5+
* @typedef {import("../generated/api").FunctionResult} FunctionResult
6+
*/
7+
8+
describe('delivery customization function', () => {
9+
it('returns no operations without configuration', () => {
10+
const result = deliveryCustomization({
11+
"cart": {
12+
"deliveryGroups": []
13+
},
14+
"deliveryCustomization": {
15+
"metafield": null
16+
}
17+
});
18+
const expected = /** @type {FunctionResult} */ ({ operations: [] });
19+
20+
expect(result).toEqual(expected);
21+
});
22+
23+
it('renames delivery options if state/province matches', () => {
24+
const result = deliveryCustomization({
25+
"cart": {
26+
"deliveryGroups": [{
27+
"deliveryAddress": {
28+
"provinceCode": "ON"
29+
},
30+
"deliveryOptions": [{
31+
"handle": "test_delivery_option",
32+
"title": "Test Delivery Option"
33+
}, {
34+
"handle": "test_delivery_option_2",
35+
"title": "Test Delivery Option 2"
36+
}]
37+
}]
38+
},
39+
"deliveryCustomization": {
40+
"metafield": {
41+
"value": "{\"stateProvinceCode\": \"ON\", \"message\": \"Test Message\"}"
42+
}
43+
}
44+
});
45+
const expected = /** @type {FunctionResult} */ ({
46+
operations: [
47+
{
48+
rename: {
49+
deliveryOptionHandle: "test_delivery_option",
50+
title: "Test Delivery Option - Test Message"
51+
}
52+
},
53+
{
54+
rename: {
55+
deliveryOptionHandle: "test_delivery_option_2",
56+
title: "Test Delivery Option 2 - Test Message"
57+
}
58+
}
59+
]
60+
});
61+
62+
expect(result).toEqual(expected);
63+
});
64+
65+
it('returns no operations if state/province code does not match', () => {
66+
const result = deliveryCustomization({
67+
"cart": {
68+
"deliveryGroups": [{
69+
"deliveryAddress": {
70+
"provinceCode": "NC"
71+
},
72+
"deliveryOptions": [{
73+
"handle": "test_delivery_option",
74+
"title": "Test Delivery Option"
75+
}]
76+
}]
77+
},
78+
"deliveryCustomization": {
79+
"metafield": {
80+
"value": "{\"stateProvinceCode\": \"ON\", \"message\": \"Test Message\"}"
81+
}
82+
}
83+
});
84+
const expected = /** @type {FunctionResult} */ ({ operations: [] });
85+
86+
expect(result).toEqual(expected);
87+
});
88+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
query Input {
2+
cart {
3+
deliveryGroups {
4+
deliveryAddress {
5+
provinceCode
6+
}
7+
deliveryOptions {
8+
handle
9+
title
10+
}
11+
}
12+
}
13+
deliveryCustomization {
14+
metafield(namespace: "$app:delivery-customization", key: "function-configuration") {
15+
value
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)