From 8d78b56fe02f8ebf4a08b5bf701e14c11b5baba2 Mon Sep 17 00:00:00 2001 From: Robin Gagnon Date: Thu, 7 Mar 2024 14:54:28 -0500 Subject: [PATCH] Revert "feat(IEX-910): Add method to fetch a drop image object (#90)" (#91) --- examples/drops/backend/src/index.ts | 5 +- .../backend/src/methods/fetch_drop_image.ts | 11 --- packages/drops/package.json | 2 +- packages/drops/src/DropsClient.ts | 67 +++++++------------ packages/drops/src/queries/DropImage.ts | 33 --------- packages/drops/src/queries/PaginatedDrop.ts | 13 +++- packages/drops/src/queries/index.ts | 1 - packages/drops/src/types/dropImage.ts | 4 +- packages/drops/src/types/index.ts | 1 - 9 files changed, 40 insertions(+), 97 deletions(-) delete mode 100644 examples/drops/backend/src/methods/fetch_drop_image.ts delete mode 100644 packages/drops/src/queries/DropImage.ts diff --git a/examples/drops/backend/src/index.ts b/examples/drops/backend/src/index.ts index 786755eb..a8743b55 100644 --- a/examples/drops/backend/src/index.ts +++ b/examples/drops/backend/src/index.ts @@ -1,9 +1,8 @@ import { DropsClient } from '@poap-xyz/drops'; import { PoapCompass, PoapDropApi } from '@poap-xyz/providers'; -import { create_drop } from './methods/create_drop'; -import { fetch_drop_image } from './methods/fetch_drop_image'; import { fetch_multiple_drops } from './methods/fetch_multiple_drops'; import { fetch_single_drop } from './methods/fetch_single_drop'; +import { create_drop } from './methods/create_drop'; import { getRequiredEnvVar } from './methods/get_required_env_var'; import dotenv from 'dotenv'; @@ -26,8 +25,6 @@ async function main(): Promise { await fetch_multiple_drops(client); // One Drop by id await fetch_single_drop(client); - // A Drop Image - await fetch_drop_image(client); // Create Drop await create_drop(client); } diff --git a/examples/drops/backend/src/methods/fetch_drop_image.ts b/examples/drops/backend/src/methods/fetch_drop_image.ts deleted file mode 100644 index 74027d01..00000000 --- a/examples/drops/backend/src/methods/fetch_drop_image.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { DropsClient } from '@poap-xyz/drops'; -import { DropImage } from '@poap-xyz/drops/dist/cjs/types'; - -export const fetch_drop_image = async (client: DropsClient): Promise => { - try { - const data: DropImage = await client.fetchDropImage(36900); - console.log(data); - } catch (error) { - console.log(error); - } -}; diff --git a/packages/drops/package.json b/packages/drops/package.json index 343ffb34..122adfcf 100644 --- a/packages/drops/package.json +++ b/packages/drops/package.json @@ -1,6 +1,6 @@ { "name": "@poap-xyz/drops", - "version": "0.1.4", + "version": "0.1.5", "description": "Drops module for the poap.js library", "main": "dist/cjs/index.cjs", "module": "dist/esm/index.mjs", diff --git a/packages/drops/src/DropsClient.ts b/packages/drops/src/DropsClient.ts index 321d257c..865a070b 100644 --- a/packages/drops/src/DropsClient.ts +++ b/packages/drops/src/DropsClient.ts @@ -8,16 +8,9 @@ import { PaginatedDropsResponse, PAGINATED_DROPS_QUERY, DropImageResponse, - DropImageQueryResponse, - DROP_IMAGE_QUERY, + DropResponse, } from './queries'; -import { - CreateDropsInput, - FetchDropsInput, - UpdateDropsInput, - DropImage, - DropImageGatewayType, -} from './types'; +import { CreateDropsInput, FetchDropsInput, UpdateDropsInput } from './types'; import { PaginatedResult, nextCursor, @@ -27,6 +20,7 @@ import { createFilter, createInFilter, } from '@poap-xyz/utils'; +import { DropImage } from './types/dropImage'; /** * Represents a client for working with POAP drops. @@ -46,21 +40,6 @@ export class DropsClient { private dropApiProvider: DropApiProvider, ) {} - /** - * Fetches the drop image object containing the original as well as the - * cropped image URLs for the specified drop id. - * - * @param id - The drop id. - * @returns The drop image object. - */ - async fetchDropImage(id: number): Promise { - const { data } = await this.compassProvider.request( - DROP_IMAGE_QUERY, - { id }, - ); - return this.mapDropImage(data.drops[0]); - } - /** * Fetches drops based on the specified input. * @@ -100,7 +79,7 @@ export class DropsClient { ); const drops = data.drops.map((drop) => { - const dropImage = this.mapDropImage(drop); + const { imageUrl, originalImageUrl } = this.computeDropImages(drop); return new Drop({ id: Number(drop.id), @@ -113,6 +92,8 @@ export class DropsClient { platform: drop.platform, locationType: drop.location_type, dropUrl: drop.drop_url, + imageUrl, + originalImageUrl, animationUrl: drop.animation_url, year: Number(drop.year), startDate: new Date(drop.start_date), @@ -130,7 +111,6 @@ export class DropsClient { : 0, expiryDate: new Date(drop.expiry_date), endDate: new Date(drop.end_date), - ...dropImage, }); }); @@ -149,7 +129,7 @@ export class DropsClient { * @returns {Promise} The newly created drop. */ async create(input: CreateDropsInput): Promise { - const response = await this.dropApiProvider.createDrop({ + const repsonse = await this.dropApiProvider.createDrop({ name: input.name, description: input.description, city: input.city, @@ -168,7 +148,7 @@ export class DropsClient { requested_codes: input.requestedCodes, private_event: input.privateEvent, }); - return this.formatDrop(response); + return this.formatDrop(repsonse); } /** @@ -225,22 +205,25 @@ export class DropsClient { }); } - /** - * Maps the response drop_image object into a DropImage object. - * - * @param drop - The response drop with drop_image object. - * @returns The mapped DropImage object. - */ - private mapDropImage(drop: DropImageResponse): DropImage { - const images = drop.drop_image?.gateways?.reduce( - (acc, gateway) => ({ ...acc, [gateway.type]: gateway.url }), + private computeDropImages(drop: DropResponse): { + imageUrl: string; + originalImageUrl: string; + } { + const dropImage = this.mapDropImage(drop.drop_image); + return { + imageUrl: dropImage?.crop || drop.image_url, + originalImageUrl: dropImage?.original || drop.image_url, + }; + } + + private mapDropImage(response?: DropImageResponse): DropImage | undefined { + if (!response) return response; + + const images = response.gateways.reduce( + (acc, gateway) => ({ ...acc, [gateway.type.toLowerCase()]: gateway.url }), {}, ); - return { - originalImageUrl: - images?.[DropImageGatewayType.ORIGINAL] || drop.image_url, - imageUrl: images?.[DropImageGatewayType.CROP] || drop.image_url, - }; + return { ...images }; } } diff --git a/packages/drops/src/queries/DropImage.ts b/packages/drops/src/queries/DropImage.ts deleted file mode 100644 index 0fd94593..00000000 --- a/packages/drops/src/queries/DropImage.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { DropImageGatewayType } from '../types/dropImage'; - -export const DROP_IMAGE_QUERY = ` - query DropImage($id: Int!) { - drops(where: { id: { _eq: $id } }) { - image_url - drop_image { - gateways { - type - url - } - } - } - } -`; - -export interface DropImageGatewayResponse { - type: DropImageGatewayType; - url: string; -} - -export interface DropImageGatewaysResponse { - gateways?: Array; -} - -export interface DropImageResponse { - image_url: string; - drop_image?: DropImageGatewaysResponse; -} - -export interface DropImageQueryResponse { - data: { drops: Array }; -} diff --git a/packages/drops/src/queries/PaginatedDrop.ts b/packages/drops/src/queries/PaginatedDrop.ts index c08eba05..78accd75 100644 --- a/packages/drops/src/queries/PaginatedDrop.ts +++ b/packages/drops/src/queries/PaginatedDrop.ts @@ -1,4 +1,4 @@ -import { DropImageGatewaysResponse } from './DropImage'; +import { DropImageGatewayType } from '../types/dropImage'; export const PAGINATED_DROPS_QUERY = ` query PaginatedDrops( @@ -48,6 +48,15 @@ export const PAGINATED_DROPS_QUERY = ` } `; +export interface DropImageGatewayResponse { + type: DropImageGatewayType; + url: string; +} + +export interface DropImageResponse { + gateways: Array; +} + export interface DropResponse { id: number; fancy_id: string; @@ -79,7 +88,7 @@ export interface DropResponse { email_claims_stats: { total: number; }; - drop_image?: DropImageGatewaysResponse; + drop_image?: DropImageResponse; } export interface PaginatedDropsResponse { diff --git a/packages/drops/src/queries/index.ts b/packages/drops/src/queries/index.ts index 5b094869..36a9e766 100644 --- a/packages/drops/src/queries/index.ts +++ b/packages/drops/src/queries/index.ts @@ -1,2 +1 @@ export * from './PaginatedDrop'; -export * from './DropImage'; diff --git a/packages/drops/src/types/dropImage.ts b/packages/drops/src/types/dropImage.ts index 1bcadae7..1f50e795 100644 --- a/packages/drops/src/types/dropImage.ts +++ b/packages/drops/src/types/dropImage.ts @@ -4,6 +4,6 @@ export enum DropImageGatewayType { } export interface DropImage { - imageUrl: string; - originalImageUrl: string; + original?: string; + crop?: string; } diff --git a/packages/drops/src/types/index.ts b/packages/drops/src/types/index.ts index 414b88cc..e3365cb9 100644 --- a/packages/drops/src/types/index.ts +++ b/packages/drops/src/types/index.ts @@ -1,2 +1 @@ export * from './input'; -export * from './dropImage';