-
Notifications
You must be signed in to change notification settings - Fork 65
Adding JavaScript functions to sample apps #169
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8094d17
rename existing payment extension to '-rust'
nickwesselman 8e53a44
add payment extension sample to graphqlrc
nickwesselman 234d797
update payment sample to latest CLI
nickwesselman 2906a13
added payment customization js and vitest
nickwesselman 4f0d50b
Update sample-apps/payment-customizations/extensions/payment-customiz…
nickwesselman fc1e06e
payments sample PR feedback
nickwesselman ef00322
move delivery customization rust function
nickwesselman e8fa91c
updated CLI for delivery sample app
nickwesselman eb068a1
better align payment js with rust
nickwesselman b945252
added js example to delivery sample app
nickwesselman 963c549
add delivery sample to graphqlrs
nickwesselman bd6b1a7
rename discounts sample rust function
nickwesselman e5acb27
upgraded cli in discounts sample
nickwesselman 169e8d6
added js to discounts sample app
nickwesselman 36b0941
update CLI to latest in samples with JS
nickwesselman b88a5ca
exclude JS extensions from cargo workspace
nickwesselman aa19fb5
make sample tests consistent with templates
nickwesselman 66ac4bf
update ui paths
nickwesselman 7869cc4
Apply suggestions from code review
nickwesselman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
}, | ||
"private": true, | ||
"workspaces": [ | ||
"*/javascript/**" | ||
"*/javascript/**", | ||
"sample-apps/*/extensions/*-js" | ||
] | ||
} |
2 changes: 2 additions & 0 deletions
2
sample-apps/delivery-customizations/extensions/delivery-customization-js/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
sample-apps/delivery-customizations/extensions/delivery-customization-js/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
...ivery-customizations/extensions/delivery-customization-js/shopify.function.extension.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
48 changes: 48 additions & 0 deletions
48
sample-apps/delivery-customizations/extensions/delivery-customization-js/src/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
88 changes: 88 additions & 0 deletions
88
sample-apps/delivery-customizations/extensions/delivery-customization-js/src/index.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
sample-apps/delivery-customizations/extensions/delivery-customization-rust/input.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.