|
| 1 | +import { getTriggerRun } from "@trigger.dev/sdk"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { |
| 4 | + SearchVariantsBodySchema, |
| 5 | + SearchVariantsSuccessResponseSchema, |
| 6 | + CreateVariantBodySchema, |
| 7 | + ProductVariantSchema, |
| 8 | + GetProductBodySchema, |
| 9 | + ProductSchema, |
| 10 | + CreateProductBodySchema, |
| 11 | + UpdateProductBodySchema, |
| 12 | + AppendProductImagesBodySchema, |
| 13 | + AppendProductImagesResponseSchema, |
| 14 | + ListCollectionsBodySchema, |
| 15 | + ListCollectionsResponseSchema, |
| 16 | + ListLocationsBodySchema, |
| 17 | + ListLocationsResponseSchema, |
| 18 | + AddProductsToCollectionBodySchema, |
| 19 | + AddProductsToCollectionResponseSchema, |
| 20 | +} from "./schemas"; |
| 21 | + |
| 22 | +export type SearchVariantsOptions = z.infer<typeof SearchVariantsBodySchema>; |
| 23 | +export type SearchVariantsResponse = z.infer< |
| 24 | + typeof SearchVariantsSuccessResponseSchema |
| 25 | +>; |
| 26 | + |
| 27 | +export async function searchProductVariants( |
| 28 | + key: string, |
| 29 | + options: SearchVariantsOptions |
| 30 | +): Promise<SearchVariantsResponse> { |
| 31 | + const run = getTriggerRun(); |
| 32 | + |
| 33 | + if (!run) { |
| 34 | + throw new Error( |
| 35 | + "Cannot call searchProductVariants outside of a trigger run" |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + const output = await run.performRequest(key, { |
| 40 | + service: "shopify", |
| 41 | + endpoint: "productVariants.search", |
| 42 | + params: options, |
| 43 | + response: { |
| 44 | + schema: SearchVariantsSuccessResponseSchema, |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + return output; |
| 49 | +} |
| 50 | + |
| 51 | +export type CreateVariantBody = z.infer<typeof CreateVariantBodySchema>; |
| 52 | + |
| 53 | +export type CreateVariantResponse = z.infer<typeof ProductVariantSchema>; |
| 54 | + |
| 55 | +export async function createProductVariant( |
| 56 | + key: string, |
| 57 | + options: CreateVariantBody |
| 58 | +): Promise<CreateVariantResponse> { |
| 59 | + const run = getTriggerRun(); |
| 60 | + |
| 61 | + if (!run) { |
| 62 | + throw new Error( |
| 63 | + "Cannot call createProductVariant outside of a trigger run" |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const output = await run.performRequest(key, { |
| 68 | + service: "shopify", |
| 69 | + endpoint: "productVariant.create", |
| 70 | + params: options, |
| 71 | + response: { |
| 72 | + schema: ProductVariantSchema, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + return output; |
| 77 | +} |
| 78 | + |
| 79 | +export type GetProductBody = z.infer<typeof GetProductBodySchema>; |
| 80 | + |
| 81 | +export type GetProductResponse = z.infer<typeof ProductSchema>; |
| 82 | + |
| 83 | +export async function getProduct( |
| 84 | + key: string, |
| 85 | + options: GetProductBody |
| 86 | +): Promise<GetProductResponse> { |
| 87 | + const run = getTriggerRun(); |
| 88 | + |
| 89 | + if (!run) { |
| 90 | + throw new Error("Cannot call getProduct outside of a trigger run"); |
| 91 | + } |
| 92 | + |
| 93 | + const output = await run.performRequest(key, { |
| 94 | + service: "shopify", |
| 95 | + endpoint: "product.get", |
| 96 | + params: options, |
| 97 | + response: { |
| 98 | + schema: ProductSchema, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + return output; |
| 103 | +} |
| 104 | + |
| 105 | +export type CreateProductBody = z.infer<typeof CreateProductBodySchema>; |
| 106 | + |
| 107 | +export type CreateProductResponse = z.infer<typeof ProductSchema>; |
| 108 | + |
| 109 | +export async function createProduct( |
| 110 | + key: string, |
| 111 | + options: CreateProductBody |
| 112 | +): Promise<CreateProductResponse> { |
| 113 | + const run = getTriggerRun(); |
| 114 | + |
| 115 | + if (!run) { |
| 116 | + throw new Error("Cannot call createProduct outside of a trigger run"); |
| 117 | + } |
| 118 | + |
| 119 | + const output = await run.performRequest(key, { |
| 120 | + service: "shopify", |
| 121 | + endpoint: "product.create", |
| 122 | + params: options, |
| 123 | + response: { |
| 124 | + schema: ProductSchema, |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + return output; |
| 129 | +} |
| 130 | + |
| 131 | +export type UpdateProductBody = z.infer<typeof UpdateProductBodySchema>; |
| 132 | + |
| 133 | +export type UpdateProductResponse = z.infer<typeof ProductSchema>; |
| 134 | + |
| 135 | +export async function updateProduct( |
| 136 | + key: string, |
| 137 | + options: UpdateProductBody |
| 138 | +): Promise<UpdateProductResponse> { |
| 139 | + const run = getTriggerRun(); |
| 140 | + |
| 141 | + if (!run) { |
| 142 | + throw new Error("Cannot call updateProduct outside of a trigger run"); |
| 143 | + } |
| 144 | + |
| 145 | + const output = await run.performRequest(key, { |
| 146 | + service: "shopify", |
| 147 | + endpoint: "product.update", |
| 148 | + params: options, |
| 149 | + response: { |
| 150 | + schema: ProductSchema, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + return output; |
| 155 | +} |
| 156 | + |
| 157 | +export type AppendProductImagesBody = z.infer< |
| 158 | + typeof AppendProductImagesBodySchema |
| 159 | +>; |
| 160 | + |
| 161 | +export type AppendProductImagesResponse = z.infer< |
| 162 | + typeof AppendProductImagesResponseSchema |
| 163 | +>; |
| 164 | + |
| 165 | +export async function appendProductImages( |
| 166 | + key: string, |
| 167 | + options: AppendProductImagesBody |
| 168 | +): Promise<AppendProductImagesResponse> { |
| 169 | + const run = getTriggerRun(); |
| 170 | + |
| 171 | + if (!run) { |
| 172 | + throw new Error("Cannot call appendProductImages outside of a trigger run"); |
| 173 | + } |
| 174 | + |
| 175 | + const output = await run.performRequest(key, { |
| 176 | + service: "shopify", |
| 177 | + endpoint: "productImages.append", |
| 178 | + params: options, |
| 179 | + response: { |
| 180 | + schema: AppendProductImagesResponseSchema, |
| 181 | + }, |
| 182 | + }); |
| 183 | + |
| 184 | + return output; |
| 185 | +} |
| 186 | + |
| 187 | +export type ListCollectionsBody = z.infer<typeof ListCollectionsBodySchema>; |
| 188 | + |
| 189 | +export type ListCollectionsResponse = z.infer< |
| 190 | + typeof ListCollectionsResponseSchema |
| 191 | +>; |
| 192 | + |
| 193 | +export async function listCollections( |
| 194 | + key: string, |
| 195 | + options: ListCollectionsBody |
| 196 | +): Promise<ListCollectionsResponse> { |
| 197 | + const run = getTriggerRun(); |
| 198 | + |
| 199 | + if (!run) { |
| 200 | + throw new Error("Cannot call listCollections outside of a trigger run"); |
| 201 | + } |
| 202 | + |
| 203 | + const output = await run.performRequest(key, { |
| 204 | + service: "shopify", |
| 205 | + endpoint: "collections.list", |
| 206 | + params: options, |
| 207 | + response: { |
| 208 | + schema: ListCollectionsResponseSchema, |
| 209 | + }, |
| 210 | + }); |
| 211 | + |
| 212 | + return output; |
| 213 | +} |
| 214 | + |
| 215 | +export type ListLocationsBody = z.infer<typeof ListLocationsBodySchema>; |
| 216 | + |
| 217 | +export type ListLocationsResponse = z.infer<typeof ListLocationsResponseSchema>; |
| 218 | + |
| 219 | +export async function listLocations( |
| 220 | + key: string, |
| 221 | + options: ListLocationsBody |
| 222 | +): Promise<ListLocationsResponse> { |
| 223 | + const run = getTriggerRun(); |
| 224 | + |
| 225 | + if (!run) { |
| 226 | + throw new Error("Cannot call listLocations outside of a trigger run"); |
| 227 | + } |
| 228 | + |
| 229 | + const output = await run.performRequest(key, { |
| 230 | + service: "shopify", |
| 231 | + endpoint: "locations.list", |
| 232 | + params: options, |
| 233 | + response: { |
| 234 | + schema: ListLocationsResponseSchema, |
| 235 | + }, |
| 236 | + }); |
| 237 | + |
| 238 | + return output; |
| 239 | +} |
| 240 | + |
| 241 | +export type AddProductsToCollectionBody = z.infer< |
| 242 | + typeof AddProductsToCollectionBodySchema |
| 243 | +>; |
| 244 | + |
| 245 | +export type AddProductsToCollectionResponse = z.infer< |
| 246 | + typeof AddProductsToCollectionResponseSchema |
| 247 | +>; |
| 248 | + |
| 249 | +export async function addProductsToCollection( |
| 250 | + key: string, |
| 251 | + options: AddProductsToCollectionBody |
| 252 | +): Promise<AddProductsToCollectionResponse> { |
| 253 | + const run = getTriggerRun(); |
| 254 | + |
| 255 | + if (!run) { |
| 256 | + throw new Error( |
| 257 | + "Cannot call addProductsToCollection outside of a trigger run" |
| 258 | + ); |
| 259 | + } |
| 260 | + |
| 261 | + const output = await run.performRequest(key, { |
| 262 | + service: "shopify", |
| 263 | + endpoint: "collection.addProducts", |
| 264 | + params: options, |
| 265 | + response: { |
| 266 | + schema: AddProductsToCollectionResponseSchema, |
| 267 | + }, |
| 268 | + }); |
| 269 | + |
| 270 | + return output; |
| 271 | +} |
0 commit comments