Skip to content

Commit

Permalink
Merge pull request #596 from Shopify/dc-local-pickup
Browse files Browse the repository at this point in the history
Update local pickup examples
  • Loading branch information
davejcameron authored Feb 24, 2025
2 parents 7afda8a + 3fa4bdb commit 7fbd371
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "{{name}}",
"description": "{{name}}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
},
"codegen": {
"schema": "schema.graphql",
"documents": "input.graphql",
"documents": "src/*.graphql",
"generates": {
"./generated/api.ts": {
"plugins": [
"typescript",
"typescript-operations"
]
}
},
"config": {
"omitOperationSuffix": true
}
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
name = "{{name}}"
type = "local_pickup_delivery_option_generator"
{% if uid %}uid = "{{ uid }}"{% endif %}
api_version = "unstable"

[build]
command = ""
path = "dist/function.wasm"
[[extensions]]
name = "t:name"
handle = "{{handle}}"
type = "function"
{% if uid %}uid = "{{ uid }}"{% endif %}

[[extensions.targeting]]
target = "purchase.local-pickup-delivery-option-generator.run"
input_query = "src/run.graphql"
export = "run"

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

[extensions.ui.paths]
create = "/"
details = "/"
Original file line number Diff line number Diff line change
@@ -1,67 +1 @@
{%- if flavor contains "vanilla-js" -%}
// @ts-check

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

/**
* @type {FunctionResult}
*/
const DELIVERY_OPTION = {
operations: [
{
add: {
title: "Main St.",
cost: 1.99,
pickupLocation: {
locationHandle: "2578303",
pickupInstruction: "Usually ready in 24 hours."
}
}
}
],
};

export default /**
* @param {InputQuery} input
* @returns {FunctionResult}
*/
(input) => {
const configuration = JSON.parse(
input?.deliveryOptionGenerator?.metafield?.value ?? "{}"
);

return DELIVERY_OPTION;
};
{%- elsif flavor contains "typescript" -%}
import {
InputQuery,
FunctionResult,
} from "../generated/api";

const DELIVERY_OPTION: FunctionResult = {
operations: [
{
add: {
title: "Main St.",
cost: 1.99,
pickupLocation: {
locationHandle: "2578303",
pickupInstruction: "Usually ready in 24 hours."
}
}
}
],
};

type Configuration = {};

export default (input: InputQuery): FunctionResult => {
const configuration: Configuration = JSON.parse(
input?.deliveryOptionGenerator?.metafield?.value ?? "{}"
);
return DELIVERY_OPTION;
};
{%- endif -%}
export * from './run';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query Input {
query RunInput {
cart {
lines {
id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{%- if flavor contains "vanilla-js" -%}
// @ts-check

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

/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
const configuration = JSON.parse(
input?.deliveryOptionGenerator?.metafield?.value ?? "{}"
);

return {
operations: [
{
add: {
title: "Main St.",
cost: 1.99,
pickupLocation: {
locationHandle: "2578303",
pickupInstruction: "Usually ready in 24 hours."
}
}
}
],
};
}
{%- elsif flavor contains "typescript" -%}
import {
RunInput,
FunctionRunResult,
} from "../generated/api";

type Configuration = {};

export function run(input: RunInput): FunctionRunResult {
const configuration: Configuration = JSON.parse(
input?.deliveryOptionGenerator?.metafield?.value ?? "{}"
);

return {
operations: [
{
add: {
title: "Main St.",
cost: 1.99,
pickupLocation: {
locationHandle: "2578303",
pickupInstruction: "Usually ready in 24 hours."
}
}
}
],
};
}
{%- endif -%}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{%- if flavor contains "vanilla-js" -%}
import { describe, it, expect } from 'vitest';
import deliveryOptionGenerator from './index';
import { run } from './run';

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

describe('local pickup delivery option generator function', () => {
it('returns a delivery option', () => {
const result = deliveryOptionGenerator({
const result = run({
cart: {
lines: [
{
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('local pickup delivery option generator function', () => {
metafield: null
}
});
const expected = /** @type {FunctionResult} */ ({
const expected = /** @type {FunctionRunResult} */ ({
operations: [
{
add: {
Expand All @@ -63,12 +64,12 @@ describe('local pickup delivery option generator function', () => {
});
{%- elsif flavor contains "typescript" -%}
import { describe, it, expect } from 'vitest';
import deliveryOptionGenerator from './index';
import { FunctionResult } from '../generated/api';
import { run } from './run';
import { RunInput, FunctionRunResult } from '../generated/api';

describe('local pickup delivery option generator function', () => {
it('returns a delivery option', () => {
const result = deliveryOptionGenerator({
const result = run({
cart: {
lines: [
{
Expand Down Expand Up @@ -103,7 +104,7 @@ describe('local pickup delivery option generator function', () => {
metafield: null
}
});
const expected: FunctionResult = {
const expected: FunctionRunResult = {
operations: [
{
add: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "{{name}}",
"description": "{{name}}"
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
name = "{{name}}"
type = "local_pickup_delivery_option_generator"
{% if uid %}uid = "{{ uid }}"{% endif %}
api_version = "unstable"

[build]
command = "cargo build --target=wasm32-wasip1 --release"
path = "target/wasm32-wasip1/release/{{name | replace: " ", "-" | downcase}}.wasm"
watch = [ "src/**/*.rs" ]
[[extensions]]
name = "t:name"
handle = "{{handle}}"
type = "function"
{% if uid %}uid = "{{ uid }}"{% endif %}

[[extensions.targeting]]
target = "purchase.local-pickup-delivery-option-generator.run"
input_query = "src/run.graphql"
export = "run"

[extensions.build]
command = "cargo build --target=wasm32-wasip1 --release"
path = "target/wasm32-wasip1/release/{{handle | replace: " ", "-" | downcase}}.wasm"
watch = [ "src/**/*.rs" ]

[ui.paths]
create = "/"
details = "/"
[extensions.ui.paths]
create = "/"
details = "/"

0 comments on commit 7fbd371

Please sign in to comment.