From eb4e0f7f2e8442cba728eded8489f116ba790ab3 Mon Sep 17 00:00:00 2001 From: David Ball Date: Mon, 26 Aug 2024 17:36:29 -0400 Subject: [PATCH] fix: Some of the release candidate API interface changes were reverted on the upstream. --- src/apiclient/override-squidex-sdk.ts | 104 ---------------- .../catalog/amazon/amazon-append-images.ts | 7 +- src/apps/catalog/common/catalog-helpers.ts | 114 +++++++----------- src/data/api-client.ts | 8 +- src/data/core/client.ts | 11 +- src/lib/page-from-models.ts | 6 +- src/scraper/amazon.ts | 4 +- 7 files changed, 63 insertions(+), 191 deletions(-) delete mode 100644 src/apiclient/override-squidex-sdk.ts diff --git a/src/apiclient/override-squidex-sdk.ts b/src/apiclient/override-squidex-sdk.ts deleted file mode 100644 index 1871946..0000000 --- a/src/apiclient/override-squidex-sdk.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { Assets } from "@squidex/squidex/api/resources/assets/client/Client.js" -import { SquidexClient } from "@squidex/squidex" -import * as environments from "@squidex/squidex/environments.js"; -import * as core from "@squidex/squidex/core/index.js"; -import { Squidex } from "@squidex/squidex"; -import urlJoin from "url-join"; -import * as errors from "@squidex/squidex/errors/index.js"; -import * as serializers from "@squidex/squidex/serialization/index.js"; -import * as fs from "fs"; -import { default as FormData } from "form-data"; - - -/** - * You can only upload one file at a time. The mime type of the file is not calculated by Squidex and is required correctly. - * @throws {@link Squidex.BadRequestError} - * @throws {@link Squidex.NotFoundError} - * @throws {@link Squidex.ContentTooLargeError} - * @throws {@link Squidex.InternalServerError} - */ -export function async customPostAsset( - file: File | fs.ReadStream, - requestOptions?: Assets.RequestOptions -): Promise { - const _request = new FormData(); - _request.append("file", file); - const _response = await (this._options.fetcher ?? core.fetcher)({ - url: urlJoin( - (await core.Supplier.get(this._options.environment)) ?? environments.SquidexEnvironment.Default, - `api/apps/${this._options.appName}/assets` - ), - method: "POST", - headers: { - Authorization: await this._getAuthorizationHeader(), - "X-Fern-Language": "JavaScript", - "X-Fern-SDK-Name": "@squidex/squidex", - "X-Fern-SDK-Version": "1.2.1", - "Content-Length": (await core.getFormDataContentLength(_request)).toString(), - }, - contentType: "multipart/form-data; boundary=" + _request.getBoundary(), - body: _request, - timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, - }); - if (_response.ok) { - return await serializers.AssetDto.parseOrThrow(_response.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - breadcrumbsPrefix: ["response"], - }); - } - - if (_response.error.reason === "status-code") { - switch (_response.error.statusCode) { - case 400: - throw new Squidex.BadRequestError( - await serializers.ErrorDto.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 404: - throw new Squidex.NotFoundError(_response.error.body); - case 413: - throw new Squidex.ContentTooLargeError( - await serializers.ErrorDto.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 500: - throw new Squidex.InternalServerError( - await serializers.ErrorDto.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - breadcrumbsPrefix: ["response"], - }) - ); - default: - throw new errors.SquidexError({ - statusCode: _response.error.statusCode, - body: _response.error.body, - }); - } - } - - switch (_response.error.reason) { - case "non-json": - throw new errors.SquidexError({ - statusCode: _response.error.statusCode, - body: _response.error.rawBody, - }); - case "timeout": - throw new errors.SquidexTimeoutError(); - case "unknown": - throw new errors.SquidexError({ - message: _response.error.errorMessage, - }); - } -}; \ No newline at end of file diff --git a/src/apps/catalog/amazon/amazon-append-images.ts b/src/apps/catalog/amazon/amazon-append-images.ts index 3edfec7..2e2cc00 100644 --- a/src/apps/catalog/amazon/amazon-append-images.ts +++ b/src/apps/catalog/amazon/amazon-append-images.ts @@ -4,6 +4,7 @@ import { getAllAssetsInFolder, isValidASIN, uploadDownloadedImageToSquidexAsAsse import { getMarketplacesUsingJsonQuery, getProductListingsUsingJsonQuery, getProductsUsingJsonQuery } from "../../../data/api-client"; import { SCHEMAS } from "../../../data/models/schemas"; import { logForCommand } from "../common/console"; +import type { ContentsPutContentRequest } from "@squidex/squidex/dist/generated/apis/ContentsApi"; export const COMMAND_NAME = 'append-images'; @@ -97,12 +98,10 @@ export const amazonAppendImagesCommand = (amazonCommand: Command) => } if (didUpdate) { log(`Listing did update, updating product listing with appended images.`); - let updatedDto = await core.client.contents.putContent({ - schema: SCHEMAS.LISTINGS, - id: listingDto.id, + let updatedDto = await core.client.contents.putContent(SCHEMAS.LISTINGS, listingDto.id, { unpublished: false, requestBody: listing as any, - }); + } as ContentsPutContentRequest as any); log(`Listing version ${updatedDto.version} stored.`); } } diff --git a/src/apps/catalog/common/catalog-helpers.ts b/src/apps/catalog/common/catalog-helpers.ts index 70e0ded..aeb0e6e 100644 --- a/src/apps/catalog/common/catalog-helpers.ts +++ b/src/apps/catalog/common/catalog-helpers.ts @@ -28,7 +28,7 @@ export function isValidASIN(asinOrNot: string) { } export async function getAmazonGetItemsRequestSchemaDto() { - return await client.schemas.getSchema({ schema: 'amazon-pa-get-items-request' }); + return await client.schemas.getSchema('amazon-pa-get-items-request'); } export async function lookupAmazonASINs(asins: string[]) { @@ -48,21 +48,17 @@ export async function lookupAmazonASINs(asins: string[]) { ] } }, requestDate: { iv: requestDate } } - let amazonGetItemDto = await client.contents.postContent({ - schema: SCHEMAS.AMAZON_GET_ITEMS, - publish: true, - requestBody: amazonGetItem as any, - }) + let amazonGetItemDto = await client.contents.postContent(SCHEMAS.AMAZON_GET_ITEMS, { ...amazonGetItem }, { publish: true }) let amazonGetItemsDto = await getContentsByIds(SCHEMAS.AMAZON_GET_ITEMS, amazonGetItemDto.id); return amazonGetItemsDto; } export async function getMarketplaceConnectionSchemaDto() { - return await client.schemas.getSchema({ schema: 'product-marketplace-connection' }); + return await client.schemas.getSchema('product-marketplace-connection'); } export async function getAmazonMarketplaceConnectionSchemaDto() { - return await client.schemas.getSchema({ schema: 'product-marketplace-connection-amazon' }) + return await client.schemas.getSchema('product-marketplace-connection-amazon') } export async function getAmazonMarketplaceDto() { @@ -100,22 +96,19 @@ export async function getBrandDtoByName(brandName: string) { } export async function getAddNewBrandDtoByName(brandName: string) { - let brandDto = await client.contents.postContent({ - schema: SCHEMAS.BRANDS, - requestBody: { - brandName: { - "en-US": brandName!, - "es-US": brandName!, - "fr-CA": brandName! - }, - slug: { - "en-US": `en-US/${slugify(brandName!, { lower: true, trim: true })}`, - "es-US": `es-US/${slugify(brandName!, { lower: true, trim: true })}`, - "fr-CA": `fr-CA/${slugify(brandName!, { lower: true, trim: true })}` - }, - } + let brandDto = await client.contents.postContent(SCHEMAS.BRANDS, { + brandName: { + "en-US": brandName!, + "es-US": brandName!, + "fr-CA": brandName! + }, + slug: { + "en-US": `en-US/${slugify(brandName!, { lower: true, trim: true })}`, + "es-US": `es-US/${slugify(brandName!, { lower: true, trim: true })}`, + "fr-CA": `fr-CA/${slugify(brandName!, { lower: true, trim: true })}` + }, }); - let brandsDto = await client.contents.getContents({ schema: SCHEMAS.BRANDS, unpublished: true, ids: brandDto.id }) as ContentsDto; + let brandsDto = await client.contents.getContents(SCHEMAS.BRANDS, { unpublished: true, ids: brandDto.id }) as ContentsDto; return brandsDto; } @@ -130,22 +123,19 @@ export async function getSellerDtoByName(sellerName: string) { } export async function getAddNewSellerDtoByName(sellerName: string) { - let sellerDto = await client.contents.postContent({ - schema: SCHEMAS.SELLERS, - requestBody: { - sellerName: { - "en-US": sellerName!, - "es-US": sellerName!, - "fr-CA": sellerName! - }, - slug: { - "en-US": `en-US/${slugify(sellerName!, { lower: true, trim: true })}`, - "es-US": `es-US/${slugify(sellerName!, { lower: true, trim: true })}`, - "fr-CA": `fr-CA/${slugify(sellerName!, { lower: true, trim: true })}` - }, - } + let sellerDto = await client.contents.postContent(SCHEMAS.SELLERS, { + sellerName: { + "en-US": sellerName!, + "es-US": sellerName!, + "fr-CA": sellerName! + }, + slug: { + "en-US": `en-US/${slugify(sellerName!, { lower: true, trim: true })}`, + "es-US": `es-US/${slugify(sellerName!, { lower: true, trim: true })}`, + "fr-CA": `fr-CA/${slugify(sellerName!, { lower: true, trim: true })}` + }, }); - let sellersDto = await client.contents.getContents({ schema: SCHEMAS.SELLERS, unpublished: true, ids: sellerDto.id }) as ContentsDto; + let sellersDto = await client.contents.getContents(SCHEMAS.SELLERS, { unpublished: true, ids: sellerDto.id }) as ContentsDto; return sellersDto; } @@ -408,16 +398,14 @@ export async function translateTags_from_en_US_to_fr_CA(tags_en_US: string[]) { } export async function getAddNewProductSubCategoryDto(parentProductCategoryId: NonLocalized, categoryName: Localized, description: Localized) { - let productCategoryDto = await client.contents.postContent({ - schema: SCHEMAS.PRODUCT_CATEGORIES, + let productCategoryDto = await client.contents.postContent(SCHEMAS.PRODUCT_CATEGORIES, { + categoryName, + description, + parentCategory: parentProductCategoryId, + }, { publish: false, - requestBody: { - categoryName, - description, - parentCategory: parentProductCategoryId, - }, }); - let productCategoriesDto = await client.contents.getContents({ schema: SCHEMAS.PRODUCT_CATEGORIES, unpublished: true, ids: productCategoryDto.id }) as ContentsDto; + let productCategoriesDto = await client.contents.getContents(SCHEMAS.PRODUCT_CATEGORIES, { unpublished: true, ids: productCategoryDto.id }) as ContentsDto; return productCategoriesDto; } @@ -448,26 +436,16 @@ export async function translateAmazonDescription_from_en_US_to_fr_CA(brandName: } export async function getAddNewProductDtoByProduct(product: Product) { - let productDto = await client.contents.postContent({ - schema: SCHEMAS.PRODUCTS, - publish: false, - requestBody: { - ...product - }, - }); - let productsDto = await client.contents.getContents({ schema: SCHEMAS.PRODUCTS, unpublished: true, ids: productDto.id }) as ContentsDto; + let productDto = await client.contents.postContent(SCHEMAS.PRODUCTS, { ...product }, { publish: false }); + let productsDto = await client.contents.getContents(SCHEMAS.PRODUCTS, { unpublished: true, ids: productDto.id }) as ContentsDto; return productsDto; } export async function getAddNewProductListingDtoByProduct(listing: Listing) { - let listingDto = await client.contents.postContent({ - schema: SCHEMAS.LISTINGS, + let listingDto = await client.contents.postContent(SCHEMAS.LISTINGS, { ...listing }, { publish: true, - requestBody: { - ...listing - } }); - let listingsDto = await client.contents.getContents({ schema: SCHEMAS.LISTINGS, unpublished: true, ids: listingDto.id }) as ContentsDto; + let listingsDto = await client.contents.getContents(SCHEMAS.LISTINGS, { unpublished: true, ids: listingDto.id }) as ContentsDto; return listingsDto; } @@ -484,7 +462,7 @@ export async function upsertAssetFolder(folderName: string, parentFolderId?: str let assetFolder; let assetFolderLookup = assetFolders.items.filter(folder => folder.folderName === folderName); if (assetFolderLookup.length === 0) { - assetFolder = await client.assets.postAssetFolder({ createAssetFolderDto: { folderName: folderName, parentId: parentFolderId }}); + assetFolder = await client.assets.postAssetFolder({ folderName: folderName, parentId: parentFolderId }); } else { assetFolder = assetFolderLookup[0]; @@ -506,20 +484,14 @@ export async function uploadDownloadedImageToSquidexAsAsset(downloadUrl: string, form.append('fileName', filename); form.append('file', blob, filename); form.append('parentId', assetFolderId); - let assetDto = await client.assets.postAsset({ file: blob, fileName: filename, fileUrl: filename, parentId: assetFolderId }, { - body: form - }); - assetDto = await client.assets.putAsset({ id: assetDto.id, annotateAssetDto: { metadata: { ...assetDto.metadata, 'amazon-url': downloadUrl }, tags: ['amazon', 'product'] } }) + let assetDto = await client.assets.postAsset({ file: blob, name: filename, url: filename, parentId: assetFolderId }, { body: form }); + assetDto = await client.assets.putAsset(assetDto.id, { metadata: { ...assetDto.metadata, 'amazon-url': downloadUrl }, tags: ['amazon', 'product'] }); // assetDto = await client.assets.putAssetParent({ id: assetDto.id, moveAssetDto: { parentId: assetFolderId } }); return assetDto; } export async function getAddNewOfferDto(offer: Offer) { - let offerDto = await client.contents.postContent({ - schema: SCHEMAS.OFFERS, - publish: true, - requestBody: offer as any, - }); - let offersDto = await client.contents.getContents({ schema: SCHEMAS.OFFERS, unpublished: true, ids: offerDto.id }) as ContentsDto; + let offerDto = await client.contents.postContent(SCHEMAS.OFFERS, { ...offer }, { publish: true }); + let offersDto = await client.contents.getContents(SCHEMAS.OFFERS, { unpublished: true, ids: offerDto.id }) as ContentsDto; return offersDto; } diff --git a/src/data/api-client.ts b/src/data/api-client.ts index 07551a0..4716f08 100644 --- a/src/data/api-client.ts +++ b/src/data/api-client.ts @@ -320,10 +320,14 @@ export async function performSyncLocalizedSlugs(logFn = console.log) { }); const MAX_TIME_TO_POST_SLUGS = 60;//s logFn("Add", batchAddSlugsQueue.length, "slugs"); - let bulkAddResult = await core.client.contents.postContents({ schema: SCHEMAS.SLUGS, importContentsDto: { datas: batchAddSlugsQueue as any, publish: true } }); + //postContents is deprecated, will loop with postContent + let bulkAddResult = [];//await core.client.contents.postContents(SCHEMAS.SLUGS, { datas: batchAddSlugsQueue as any, publish: true }); + for (let bar = 0; bar < batchAddSlugsQueue.length; bar++) { + bulkAddResult.push(await core.client.contents.postContent(SCHEMAS.SLUGS, batchAddSlugsQueue[bar] as any, { publish: true })); + } logFn("Remove by id", batchRemoveSlugsQueue.length, "slugs"); batchRemoveSlugsQueue.forEach(async (removeId) => { - await core.client.contents.deleteContent({ schema: SCHEMAS.SLUGS, id: removeId }) + await core.client.contents.deleteContent(SCHEMAS.SLUGS, removeId) }) logFn("Finish sync localized slugs.") } diff --git a/src/data/core/client.ts b/src/data/core/client.ts index e8bc92e..e0ea15c 100644 --- a/src/data/core/client.ts +++ b/src/data/core/client.ts @@ -1,5 +1,6 @@ import { config } from "../../config.js"; import { SquidexClient } from "@squidex/squidex"; +import { InMemoryTokenStore } from "@squidex/squidex/dist/wrapper/SquidexClient.js"; import type { ContentsDto } from "../internals/ContentsDtoT.js"; import type { SupportedLocales } from "../internals/LocalizedT.js"; import type { SCHEMAS } from "../models/schemas.js"; @@ -9,7 +10,7 @@ export const client = new SquidexClient({ clientId: config.squidexClientId!, clientSecret: config.squidexClientSecret!, environment: config.squidexEnvironment!, - tokenStore: new SquidexClient.InMemoryTokenStore(), + tokenStore: new InMemoryTokenStore(), // tokenStore: new SquidexStorageTokenStore() // Keep the tokens in the local store. // tokenStore: new SquidexStorageTokenStore(sessionStorage, "CustomKey") }); @@ -19,21 +20,21 @@ export const TIMEOUT_IN_SECONDS = 10; /** Asset Handling */ export const getAssetById = async (assetId: string) => ( - await client.assets.getAsset({ id: assetId }) + await client.assets.getAsset(assetId) ); /** Generic Content Handling */ export const getAllContents = async (schema: SCHEMAS|string) => ( - await client.contents.getContents({ schema }) + await client.contents.getContents(schema, { }) ) as ContentsDto; export const getContentsByIds = async (schema: SCHEMAS|string, ids: string) => ( - await client.contents.getContents({ schema, ids }) + await client.contents.getContents(schema, { ids }) ) as ContentsDto; export const getContentsUsingJsonQuery = async (schema: SCHEMAS|string, jsonQuery: string|undefined = undefined) => ( - await client.contents.getContents({ schema, q: jsonQuery }) + await client.contents.getContents(schema, { q: jsonQuery }) ) as ContentsDto; export const getContentsByLangSlug = async (schema: SCHEMAS|string, forLang: SupportedLocales|string, slug: string) => ( diff --git a/src/lib/page-from-models.ts b/src/lib/page-from-models.ts index ad969b8..40ca5a7 100644 --- a/src/lib/page-from-models.ts +++ b/src/lib/page-from-models.ts @@ -76,7 +76,7 @@ export const genericPageForBrand = ({ brand, brandId, homePageId }: PageForBrand schemaId: '', schemaName: '', keywords: brand.brandName['en-US'], - metaDescription: brand.shortDescription['en-US'], + metaDescription: brand.shortDescription!['en-US'], metaImage: '', metaSocial: [], metaTitle: brand.brandName['en-US'], @@ -85,7 +85,7 @@ export const genericPageForBrand = ({ brand, brandId, homePageId }: PageForBrand schemaId: '', schemaName: '', keywords: brand.brandName['es-US'], - metaDescription: brand.shortDescription['es-US'], + metaDescription: brand.shortDescription!['es-US'], metaImage: '', metaSocial: [], metaTitle: brand.brandName['es-US'], @@ -94,7 +94,7 @@ export const genericPageForBrand = ({ brand, brandId, homePageId }: PageForBrand schemaId: '', schemaName: '', keywords: brand.brandName['fr-CA'], - metaDescription: brand.shortDescription['fr-CA'], + metaDescription: brand.shortDescription!['fr-CA'], metaImage: '', metaSocial: [], metaTitle: brand.brandName['fr-CA'], diff --git a/src/scraper/amazon.ts b/src/scraper/amazon.ts index 14ec63c..5539589 100644 --- a/src/scraper/amazon.ts +++ b/src/scraper/amazon.ts @@ -3,8 +3,8 @@ */ import cheerio, { type CheerioAPI } from 'cheerio'; -import { type AmazonProductDetails } from '../data/products/amazon-product-details'; -import { type ProductAttribute } from '../data/products/product-attribute'; +import { type AmazonProductDetails } from '../old-data/products/amazon-product-details'; +import { type ProductAttribute } from '../old-data/products/product-attribute'; import { parseNumberFromSelector } from './utils'; /**