fix: Some of the release candidate API interface changes were reverted on the upstream.

This commit is contained in:
David Ball 2024-08-26 17:36:29 -04:00
parent 989b1c8af4
commit eb4e0f7f2e
7 changed files with 63 additions and 191 deletions

View File

@ -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<Squidex.AssetDto> {
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,
});
}
};

View File

@ -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.`);
}
}

View File

@ -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<AmazonGetItem>(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,9 +96,7 @@ export async function getBrandDtoByName(brandName: string) {
}
export async function getAddNewBrandDtoByName(brandName: string) {
let brandDto = await client.contents.postContent({
schema: SCHEMAS.BRANDS,
requestBody: {
let brandDto = await client.contents.postContent(SCHEMAS.BRANDS, {
brandName: {
"en-US": brandName!,
"es-US": brandName!,
@ -113,9 +107,8 @@ export async function getAddNewBrandDtoByName(brandName: string) {
"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<Brand>;
let brandsDto = await client.contents.getContents(SCHEMAS.BRANDS, { unpublished: true, ids: brandDto.id }) as ContentsDto<Brand>;
return brandsDto;
}
@ -130,9 +123,7 @@ export async function getSellerDtoByName(sellerName: string) {
}
export async function getAddNewSellerDtoByName(sellerName: string) {
let sellerDto = await client.contents.postContent({
schema: SCHEMAS.SELLERS,
requestBody: {
let sellerDto = await client.contents.postContent(SCHEMAS.SELLERS, {
sellerName: {
"en-US": sellerName!,
"es-US": sellerName!,
@ -143,9 +134,8 @@ export async function getAddNewSellerDtoByName(sellerName: string) {
"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<Seller>;
let sellersDto = await client.contents.getContents(SCHEMAS.SELLERS, { unpublished: true, ids: sellerDto.id }) as ContentsDto<Seller>;
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<string[]>, categoryName: Localized<string>, description: Localized<string>) {
let productCategoryDto = await client.contents.postContent({
schema: SCHEMAS.PRODUCT_CATEGORIES,
publish: false,
requestBody: {
let productCategoryDto = await client.contents.postContent(SCHEMAS.PRODUCT_CATEGORIES, {
categoryName,
description,
parentCategory: parentProductCategoryId,
},
}, {
publish: false,
});
let productCategoriesDto = await client.contents.getContents({ schema: SCHEMAS.PRODUCT_CATEGORIES, unpublished: true, ids: productCategoryDto.id }) as ContentsDto<ProductCategory>;
let productCategoriesDto = await client.contents.getContents(SCHEMAS.PRODUCT_CATEGORIES, { unpublished: true, ids: productCategoryDto.id }) as ContentsDto<ProductCategory>;
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<Product>;
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<Product>;
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<Listing>;
let listingsDto = await client.contents.getContents(SCHEMAS.LISTINGS, { unpublished: true, ids: listingDto.id }) as ContentsDto<Listing>;
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<ProductCategory>;
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<ProductCategory>;
return offersDto;
}

View File

@ -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.")
}

View File

@ -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 <T>(schema: SCHEMAS|string) => (
await client.contents.getContents({ schema })
await client.contents.getContents(schema, { })
) as ContentsDto<T>;
export const getContentsByIds = async <T>(schema: SCHEMAS|string, ids: string) => (
await client.contents.getContents({ schema, ids })
await client.contents.getContents(schema, { ids })
) as ContentsDto<T>;
export const getContentsUsingJsonQuery = async <T>(schema: SCHEMAS|string, jsonQuery: string|undefined = undefined) => (
await client.contents.getContents({ schema, q: jsonQuery })
await client.contents.getContents(schema, { q: jsonQuery })
) as ContentsDto<T>;
export const getContentsByLangSlug = async <T>(schema: SCHEMAS|string, forLang: SupportedLocales|string, slug: string) => (

View File

@ -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'],

View File

@ -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';
/**