omg this is working

This commit is contained in:
Ben Miller
2024-11-13 23:43:32 -07:00
parent fb86c9c96d
commit a5c0a1176d
22 changed files with 2152 additions and 53 deletions

View File

@ -482,18 +482,19 @@ export class Shop {
GetProducts() {
let done = false
let query = ""
let cursor = ""
let fields = ["id", "title"]
var response = {
content: {},
headers: {},
}
let products: Product[] = []
let products: ShopifyProduct[] = []
do {
let query = new ProductsQuery(fields, cursor)
response = this.shopifyGraphQLAPI(query.JSON)
let pq = new ShopifyProductsQuery(query, fields, cursor)
response = this.shopifyGraphQLAPI(pq.JSON)
console.log(response)
let productsResponse = new ProductsResponse(response.content)
let productsResponse = new ShopifyProductsResponse(response.content)
if (productsResponse.products.edges.length <= 0) {
console.log("no products returned")
done = true
@ -502,7 +503,7 @@ export class Shop {
for (let i = 0; i < productsResponse.products.edges.length; i++) {
let edge = productsResponse.products.edges[i]
console.log(JSON.stringify(edge))
let p = new Product()
let p = new ShopifyProduct()
Object.assign(edge.node, p)
products.push(p)
}
@ -609,7 +610,7 @@ export class Order {
}
}
export class Product {
export class ShopifyProduct {
body_html: string
created_at: Date
handle: string
@ -678,7 +679,7 @@ class Products {
}
class ProductEdge {
node: Product
node: ShopifyProduct
cursor: string
}
@ -689,12 +690,14 @@ class PageInfo {
endCursor: string
}
class ProductsQuery {
export class ShopifyProductsQuery {
GQL: string
JSON: JSON
constructor(
query: string = "",
fields: string[] = ["id", "title", "handle"],
cursor: string = ""
cursor: string = "",
pageSize: number = 10,
) {
let cursorText: string
if (cursor == "") {
@ -702,8 +705,14 @@ class ProductsQuery {
} else {
cursorText = `, after: "${cursor}"`
}
let queryText: string
if (query == "") {
queryText = ""
} else {
queryText = `, query: "${query}"`
}
this.GQL = `{
products(first: 10${cursorText}) {
products(first: ${pageSize}${cursorText}${queryText}) {
edges {
node { ${fields.join(" ")} }
}
@ -719,7 +728,7 @@ class ProductsQuery {
}
}
class ProductsResponse {
export class ShopifyProductsResponse {
products: Products
constructor(response: {}) {
this.products = response["data"]["products"]
@ -729,4 +738,16 @@ class ProductsResponse {
function formatGqlForJSON(gql: string) {
let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ")
return JSON.stringify(singleLine)
}
}
export function runShopifyOrders() {
let shop = new Shop()
shop.RunOrders()
}
export function getShopifyProducts() {
let shop = new Shop()
shop.GetProducts()
}
(global as any)