Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions components/afosto/actions/add-item-to-cart/add-item-to-cart.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import app from "../../afosto.app.mjs";

export default {
key: "afosto-add-item-to-cart",
name: "Add Item to Cart",
description: "Add an item to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/add-and-remove-items/)",
version: "0.0.1",
type: "action",
props: {
app,
sku: {
type: "string",
label: "SKU",
description: "The SKU of the item to add to the cart.",
},
quantity: {
type: "integer",
label: "Quantity",
description: "The quantity of the item to add to the cart.",
},
cartId: {
propDefinition: [
app,
"cartId",
],
},
},
async run({ $ }) {
try {
const response = await this.app.query({
$,
data: JSON.stringify({
query: `mutation AddItemToCart(
$input: AddItemsToCartInput!
) {
addItemsToCart(input: $input) {
cart {
id
items {
ids
label
brand
mpn
gtin
image
hs_code
country_of_origin
url
sku
quantity
subtotal
total
parent_id
}
}
}
}`,
variables: {
input: {
cart_id: this.cartId,
items: [
{
sku: this.sku,
quantity: this.quantity,
},
],
},
},
}),
});

$.export("$summary", `Successfully added item to cart with SKU: ${response.data.addItemsToCart.cart.items[0].sku}`);
return response.data.addItemsToCart.cart;
} catch ({ response }) {
throw new Error(`${response.data?.errors?.[0]?.message}`);
}
},
};
66 changes: 66 additions & 0 deletions components/afosto/actions/add-note-to-cart/add-note-to-cart.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import app from "../../afosto.app.mjs";

export default {
key: "afosto-add-note-to-cart",
name: "Add Note to Cart",
description: "Add a note to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/checkout-summary/)",
version: "0.0.1",
type: "action",
props: {
app,
cartId: {
propDefinition: [
app,
"cartId",
],
},
note: {
type: "string",
label: "Note",
description: "The note to add to the cart",
},
},
async run({ $ }) {
try {
const variables = {
cartId: this.cartId,
note: this.note,
};

const response = await this.app.query({
$,
maxBodyLength: Infinity,
headers: {
"DisablePreParseMultipartForm": "true",
},
data: JSON.stringify({
query: `mutation SetNoteForCart (
$cartId: String!
$note: String!
) {
setNoteForCart(input: { cart_id: $cartId, note: $note }) {
cart {
id
number
total
subtotal
total_excluding_vat
currency
is_including_vat
is_vat_shifted
created_at
updated_at
}
}
}`,
variables,
}),
});

$.export("$summary", `Successfully added note to cart with ID: ${this.cartId}`);
return response.data.setNoteForCart.cart;
} catch (error) {
throw new Error(`${error.errors?.[0]?.message || "An unknown error occurred"}`);
}
},
};
45 changes: 45 additions & 0 deletions components/afosto/actions/confirm-cart/confirm-cart.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import app from "../../afosto.app.mjs";

export default {
key: "afosto-confirm-cart",
name: "Confirm Cart",
description: "Confirm a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/payment-process/)",
version: "0.0.1",
type: "action",
props: {
app,
cartId: {
propDefinition: [
app,
"cartId",
],
},
},
async run({ $ }) {
try {
const response = await this.app.query({
$,
data: JSON.stringify({
query: `mutation ConfirmCart (
$cartId: String!
) {
confirmCart(input: { cart_id: $cartId }) {
order {
id
}
}
}
`,
variables: {
cartId: this.cartId,
},
}),
});

$.export("$summary", `Successfully confirmed cart with ID: ${response.data.confirmCart.order.id}`);
return response.data.confirmCart.order;
} catch ({ response }) {
throw new Error(`${response.data?.errors?.[0]?.message}`);
}
},
};
41 changes: 41 additions & 0 deletions components/afosto/actions/create-cart/create-cart.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import app from "../../afosto.app.mjs";

export default {
key: "afosto-create-cart",
name: "Create Cart",
description: "Create a new cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/create-a-cart/)",
version: "0.0.1",
type: "action",
props: {
app,
},
async run({ $ }) {
try {
const response = await this.app.query({
$,
data: JSON.stringify({
query: `mutation createCart {
createCart(input: {}) {
cart {
id
number
total
subtotal
total_excluding_vat
currency
is_including_vat
is_vat_shifted
created_at
}
}
}`,
}),
});

$.export("$summary", `Successfully created cart with ID: ${response.data.createCart.cart.id}`);
return response.data.createCart.cart;
} catch ({ response }) {
throw new Error(`${response.data?.errors?.[0]?.message}`);
}
},
};
37 changes: 33 additions & 4 deletions components/afosto/afosto.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "afosto",
propDefinitions: {},
propDefinitions: {
cartId: {
type: "string",
label: "Cart ID",
description: "The ID of the cart",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_headers(headers = {}) {
return {
"Authorization": `Bearer ${this.$auth.auth_token}`,
"Content-Type": "application/json",
...headers,
};
},
_baseUrl() {
return "https://afosto.app/graphql";
},
_makeRequest({
$ = this, path = "", headers, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(headers),
...opts,
});
},
query(opts = {}) {
return this._makeRequest({
method: "POST",
...opts,
});
},
},
};
24 changes: 24 additions & 0 deletions components/afosto/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const parseObject = (obj) => {
if (!obj) return undefined;

if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};
7 changes: 5 additions & 2 deletions components/afosto/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/afosto",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Afosto Components",
"main": "afosto.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.1.0"
}
}
}
20 changes: 14 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading