Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding JavaScript functions to sample apps #169

Merged
merged 19 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .graphqlrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function getProjects(path) {

const projects = {
...getProjects("sample-apps/discounts/extensions"),
...getProjects("sample-apps/payment-customizations/extensions"),
...getProjects("sample-apps/delivery-customizations/extensions"),
...getProjects("checkout/rust/delivery-customization"),
...getProjects("checkout/rust/payment-customization"),
...getProjects("checkout/javascript/delivery-customization"),
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ members = [
"*/rust/*/*",
"sample-apps/*/extensions/*",
]
exclude = [
# Excludes do not support globbing -- https://github.com/rust-lang/cargo/issues/11405
"sample-apps/delivery-customizations/extensions/delivery-customization-js",
"sample-apps/discounts/extensions/product-discount-js",
"sample-apps/payment-customizations/extensions/payment-customization-js/",
]
nickwesselman marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"private": true,
"workspaces": [
"*/javascript/**"
"*/javascript/**",
"sample-apps/*/extensions/*-js"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
generated
nickwesselman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "delivery-customization",
"version": "0.0.1",
"license": "UNLICENSED",
"scripts": {
"shopify": "npm exec -- shopify",
"typegen": "npm exec -- shopify app function typegen",
"build": "npm exec -- shopify app function build",
"preview": "npm exec -- shopify app function run",
"test": "vitest"
},
"codegen": {
"schema": "schema.graphql",
"documents": "input.graphql",
"generates": {
"./generated/api.ts": {
"plugins": [
"typescript",
"typescript-operations"
]
}
}
},
"devDependencies": {
"vitest": "^0.29.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "delivery-customization-js"
type = "delivery_customization"
api_version = "2023-04"

[build]
command = ""
path = "dist/function.wasm"

[ui.paths]
create = "/delivery-customization/:functionId/new"
details = "/delivery-customization/:functionId/:id"
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// @ts-check

/**
* @typedef {import("../generated/api").InputQuery} InputQuery
* @typedef {import("../generated/api").FunctionResult} FunctionResult
* @typedef {import("../generated/api").Operation} Operation
*/

/**
* @type {FunctionResult}
*/
const NO_CHANGES = {
operations: [],
};

export default /**
* @param {InputQuery} input
* @returns {FunctionResult}
*/
(input) => {
/**
* @type {{
* stateProvinceCode: string
* message: number
* }}
*/
const configuration = JSON.parse(
input?.deliveryCustomization?.metafield?.value ?? "{}"
);
if (!configuration.stateProvinceCode || !configuration.message) {
return NO_CHANGES;
}

let toRename = input.cart.deliveryGroups
.filter(group => group.deliveryAddress?.provinceCode &&
group.deliveryAddress.provinceCode == configuration.stateProvinceCode)
.flatMap(group => group.deliveryOptions)
.map(option => /** @type {Operation} */({
rename: {
deliveryOptionHandle: option.handle,
title: option.title ? `${option.title} - ${configuration.message}` : configuration.message
}
}));

return {
operations: toRename
};
};
nickwesselman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, it, expect } from 'vitest';
import deliveryCustomization from './index';

/**
* @typedef {import("../generated/api").FunctionResult} FunctionResult
*/

describe('delivery customization function', () => {
it('returns no operations without configuration', () => {
const result = deliveryCustomization({
"cart": {
"deliveryGroups": []
},
"deliveryCustomization": {
"metafield": null
}
});
const expected = /** @type {FunctionResult} */ ({ operations: [] });

expect(result).toEqual(expected);
});

it('renames delivery options if state/province matches', () => {
const result = deliveryCustomization({
"cart": {
"deliveryGroups": [{
"deliveryAddress": {
"provinceCode": "ON"
},
"deliveryOptions": [{
"handle": "test_delivery_option",
"title": "Test Delivery Option"
}, {
"handle": "test_delivery_option_2",
"title": "Test Delivery Option 2"
}]
}]
},
"deliveryCustomization": {
"metafield": {
"value": "{\"stateProvinceCode\": \"ON\", \"message\": \"Test Message\"}"
}
}
});
const expected = /** @type {FunctionResult} */ ({
operations: [
{
rename: {
deliveryOptionHandle: "test_delivery_option",
title: "Test Delivery Option - Test Message"
}
},
{
rename: {
deliveryOptionHandle: "test_delivery_option_2",
title: "Test Delivery Option 2 - Test Message"
}
}
]
});

expect(result).toEqual(expected);
});

it('returns no operations if state/province code does not match', () => {
const result = deliveryCustomization({
"cart": {
"deliveryGroups": [{
"deliveryAddress": {
"provinceCode": "NC"
},
"deliveryOptions": [{
"handle": "test_delivery_option",
"title": "Test Delivery Option"
}]
}]
},
"deliveryCustomization": {
"metafield": {
"value": "{\"stateProvinceCode\": \"ON\", \"message\": \"Test Message\"}"
}
}
});
const expected = /** @type {FunctionResult} */ ({ operations: [] });

expect(result).toEqual(expected);
});
});
nickwesselman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
query Input {
cart {
deliveryGroups {
deliveryAddress {
provinceCode
}
deliveryOptions {
handle
title
}
}
}
deliveryCustomization {
metafield(namespace: "$app:delivery-customization", key: "function-configuration") {
value
}
}
}
Loading