Last active
May 16, 2023 18:32
-
-
Save airhorns/87f8398b2a8aa14473079e7c22f09355 to your computer and use it in GitHub Desktop.
Gadget nextgen code snippets
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
import { assignInputs, validateAndSave } from "gadget"; | |
/** | |
* Run Action code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function run({ api, record, inputs }) { | |
assignInputs(record, inputs); | |
await validateAndSave(record); | |
} | |
/** | |
* On Action Success code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function onSuccess({ api, record, inputs }) { | |
// access the post record's data with the record object | |
// console.log(record.id); | |
// access other models in your Gadget application with the api object | |
// const otherRecords = await api.post.findMany({first: 10}); | |
// use passed in inputs | |
// const foobar = inputs.foo + inputs.bar; | |
// make API calls to other systems with fetch or libraries from npm | |
// await fetch("https://some-other-api.com/v1/api", { method: "POST", body: record.toJSON() }); | |
} |
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
import { assignInputs } from "gadget"; | |
import { slugify } from "lodash"; | |
/** | |
* Run Action code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function run({ api, record, inputs }) { | |
assignInputs(record, inputs); | |
record.slug ??= slugify(record.title); | |
await validateAndSave(record); | |
} | |
run.transactional = true; | |
/** | |
* On Action Success code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function onSuccess({ api, record, inputs }) { | |
await fetch("https://some-other-api.com/v1/api", { | |
method: "POST", | |
body: record.toJSON(), | |
headers: { | |
'content-type': "application/json" | |
} | |
}); | |
} | |
success.transactional = true; | |
export const timeout = 15000; // milliseconds | |
export const inputs = { | |
type: "object"; | |
properties: { | |
foo: { type: "string } | |
} | |
}; |
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
import { assignInputs } from "gadget"; | |
/** | |
* Run Action code for create on shopifyProduct | |
* @param { import("gadget").CreateShopifyProductActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function run({ api, record, inputs, connections }) { | |
connections.shopify.ensureCorrectTenancy(record); | |
assignInputs(record, inputs); | |
await validateAndSave(record); | |
} | |
/** | |
* On Action Success code for create on shopifyProduct | |
* @param { import("gadget").CreateShopifyProductActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function onSuccess({ api, record, inputs, connections }) { | |
await connections.shopify.current.product.update(record.id, { | |
tags: ["some", "new", "tags"].join(',') | |
}); | |
} |
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
import { assignInputs } from "gadget"; | |
/** | |
* Run Action code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function run({ api, inputs, validator }) { | |
return await api.transaction(async () => { | |
const record = await api.post.initialize(); | |
assignInputs(record, inputs); | |
await validator.validate(record); | |
return await api.internal.post.create(record); | |
}); | |
} | |
/** | |
* On Action Success code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function onSuccess({ api, record, inputs }) { | |
// access the post record's data with the record object | |
// console.log(record.id); | |
// access other models in your Gadget application with the api object | |
// const otherRecords = await api.post.findMany({first: 10}); | |
// use passed in inputs | |
// const foobar = inputs.foo + inputs.bar; | |
// make API calls to other systems with fetch or libraries from npm | |
// await fetch("https://some-other-api.com/v1/api", { method: "POST", body: record.toJSON() }); | |
} |
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
import { assignInputs } from "gadget"; | |
import { slugify } from "lodash"; | |
/** | |
* Run Action code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function run({ api, inputs, validator }) { | |
return await api.transaction(async () => { | |
const record = await api.post.initialize(); | |
assignInputs(record, inputs); | |
record.slug ??= slugify(record.title); | |
await validator.validate(record); | |
return await api.internal.post.create(record); | |
}); | |
} | |
run.transactional = false; | |
/** | |
* On Action Success code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function onSuccess({ api, record, inputs }) { | |
await fetch("https://some-other-api.com/v1/api", { | |
method: "POST", | |
body: record.toJSON(), | |
headers: { | |
'content-type': "application/json" | |
} | |
}); | |
} | |
onSuccess.transactional = true; | |
export const timeout = 15000; // milliseconds | |
export const inputs = { | |
type: "object"; | |
properties: { | |
foo: { type: "string } | |
} | |
}; |
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
import { assignInputs } from "gadget"; | |
/** | |
* Run Action code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function run({ api, inputs, validator, connections }) { | |
return await api.transaction(async () => { | |
const record = await api.post.initialize(); | |
assignInputs(record, inputs); | |
record.shopId = connections.shopify.current.shopId; | |
await validator.validate(record); | |
return await api.internal.shopifyProduct.create(record); | |
}); | |
} | |
/** | |
* On Action Success code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function onSuccess({ api, record, inputs }) { | |
await connections.shopify.current.product.update(record.id, { | |
tags: ["some", "new", "tags"].join(',') | |
}); | |
} |
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
import { assignInputs } from "gadget"; | |
/** | |
* Run Action code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function run({ api, inputs, validator, connections }) { | |
return await api.transaction(async () => { | |
const record = await api.post.findById(inputs.id); | |
connections.shopify.current.assertCorrectShopId(record); | |
assignInputs(record, inputs); | |
await validator.validate(record); | |
return await api.internal.shopifyProduct.update(record.id, record); | |
}); | |
} | |
/** | |
* On Action Success code for create on post | |
* @param { import("gadget").CreatePostActionContext } context - Everything for running this action handler, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context | |
*/ | |
export async function onSuccess({ api, record, inputs }) { | |
await connections.shopify.current.product.update(record.id, { | |
tags: ["some", "new", "tags"].join(',') | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment