Skip to content

Commit

Permalink
Refactor Moment Create Medias (#61)
Browse files Browse the repository at this point in the history
* move ts-node as dev-dependency

* Add medias as optional create moment input
  • Loading branch information
nacho9900 authored Aug 25, 2023
1 parent 23b3d18 commit 7c12674
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 57 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ docs/.yarn/
!.yarn/versions
.next
.idea
.vscode
.vscode

# Ignore Typescript definitions file extension
*.d.ts
10 changes: 7 additions & 3 deletions examples/moments/backend/src/methods/create_moment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable max-statements */
import { MomentsClient, CreateMomentInput } from '@poap-xyz/moments';
import fs from 'fs';
import mime from 'mime';
Expand All @@ -18,9 +17,14 @@ export const create_moment = async (client: MomentsClient): Promise<void> => {
* The Token ID related to the moment (Optional)
*/
tokenId: 6568008,
fileBinary: fileBuffer,
author: '0x82AB2941Cf555CED5ad7Ed232a5B5f6083815FBC',
fileType: mimeType,
medias: [
{
fileBinary: fileBuffer,
fileType: mimeType,
},
],
description: 'This is a description',
};

try {
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-typescript2": "^0.31.1",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typedoc": "^0.23.28",
"typescript": "^5.0.2"
},
"workspaces": [
"packages/*",
"examples/**"
],
"packageManager": "[email protected]",
"dependencies": {
"ts-node": "^10.9.1"
}
"packageManager": "[email protected]"
}
6 changes: 3 additions & 3 deletions packages/drops/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/drops",
"version": "0.0.35",
"version": "0.0.36",
"description": "Drops module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,7 +26,7 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.0.35",
"@poap-xyz/utils": "0.0.35"
"@poap-xyz/providers": "0.0.36",
"@poap-xyz/utils": "0.0.36"
}
}
6 changes: 3 additions & 3 deletions packages/moments/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/moments",
"version": "0.0.35",
"version": "0.0.36",
"description": "Moments module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,7 +26,7 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.0.35",
"@poap-xyz/utils": "0.0.35"
"@poap-xyz/providers": "0.0.36",
"@poap-xyz/utils": "0.0.36"
}
}
21 changes: 19 additions & 2 deletions packages/moments/src/client/MomentsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('MomentsClient', () => {
const FILE_TYPE = 'image/png';
const MEDIA_KEY = 'this-is-a-media-key';
const MEDIA_UPLOAD_URL = 'this-is-a-media-upload-url';
const DESCRIPTION = 'This is a description';

let poapMomentsAPIMocked: MockProxy<PoapMomentsApi>;
let compassProviderMocked: MockProxy<PoapCompass>;
Expand All @@ -34,10 +35,15 @@ describe('MomentsClient', () => {
const inputs: CreateMomentInput = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
fileBinary: FILE,
fileType: FILE_TYPE,
medias: [
{
fileBinary: FILE,
fileType: FILE_TYPE,
},
],
author: AUTHOR,
onStepUpdate,
description: DESCRIPTION,
};
poapMomentsAPIMocked.createMoment.mockResolvedValue({
id: MOMENT_ID,
Expand All @@ -51,6 +57,14 @@ describe('MomentsClient', () => {
url: MEDIA_UPLOAD_URL,
});

const EXPECTED_MOMENT_CREATE_INPUT = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
author: AUTHOR,
description: DESCRIPTION,
mediaKeys: [MEDIA_KEY],
};

// WHEN
const moment = await client.createMoment(inputs);

Expand All @@ -59,6 +73,9 @@ describe('MomentsClient', () => {
expect(moment.author).toBe(AUTHOR);
expect(moment.dropId).toBe(DROP_ID);
expect(moment.tokenId).toBe(TOKEN_ID);
expect(poapMomentsAPIMocked.createMoment).toHaveBeenCalledWith(
EXPECTED_MOMENT_CREATE_INPUT,
);
expect(onStepUpdate).toHaveBeenCalledWith(
CreateSteps.REQUESTING_MEDIA_UPLOAD_URL,
);
Expand Down
75 changes: 53 additions & 22 deletions packages/moments/src/client/MomentsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,46 @@ import {
import { CreateMomentInput } from './dtos/create/CreateInput';
import { CreateSteps } from './dtos/create/CreateSteps';
import { FetchMomentsInput } from './dtos/fetch/FetchMomentsInput';
import { CreateMedia } from './dtos/create/CreateMedia';

export class MomentsClient {
private static readonly DEFAULT_ON_STEP_UPDATE = (): void => {
//do nothing
};

constructor(
private poapMomentsApi: PoapMomentsApi,
private CompassProvider: CompassProvider,
) {}

public async createMoment(input: CreateMomentInput): Promise<Moment> {
const defaultOnStepUpdate = (): void => {
//do nothing
};
const onStepUpdate = input.onStepUpdate || defaultOnStepUpdate;
void onStepUpdate(CreateSteps.REQUESTING_MEDIA_UPLOAD_URL);
const { url, key } = await this.poapMomentsApi.getSignedUrl();
void onStepUpdate(CreateSteps.UPLOADING_MEDIA);
await this.poapMomentsApi.uploadFile(
input.fileBinary,
url,
input.fileType,
input.onFileUploadProgress,
);
void onStepUpdate(CreateSteps.UPLOADING_MEDIA_METADATA);
// we will be adding metadata to the media in the future
void onStepUpdate(CreateSteps.PROCESSING_MEDIA);
try {
await this.poapMomentsApi.waitForMediaProcessing(key, input.timeOut);
} catch (error) {
void onStepUpdate(CreateSteps.PROCESSING_MEDIA_ERROR);
throw error;
if (input.medias && input.medias.length > 1) {
// TODO: implement multiple medias
throw new Error('Multiple medias not supported yet');
}

const onStepUpdate =
input.onStepUpdate || MomentsClient.DEFAULT_ON_STEP_UPDATE;

const mediaKeys: string[] = [];
if (input.medias && input.medias.length > 0) {
const media = input.medias[0];
const key = await this.createMedia(
media,
onStepUpdate,
input.onFileUploadProgress,
input.timeOut,
);
mediaKeys.push(key);
}

void onStepUpdate(CreateSteps.UPLOADING_MOMENT);
const response = await this.poapMomentsApi.createMoment({
dropId: input.dropId,
author: input.author,
mediaKeys: [key],
tokenId: input.tokenId,
description: input.description,
mediaKeys,
});
void onStepUpdate(CreateSteps.FINISHED);
return new Moment(
Expand All @@ -66,6 +69,34 @@ export class MomentsClient {
);
}

private async createMedia(
media: CreateMedia,
onStepUpdate: (step: CreateSteps) => void | Promise<void>,
onFileUploadProgress?: (progress: number) => void | Promise<void>,
timeOut?: number,
): Promise<string> {
void onStepUpdate(CreateSteps.REQUESTING_MEDIA_UPLOAD_URL);
const { url, key } = await this.poapMomentsApi.getSignedUrl();
void onStepUpdate(CreateSteps.UPLOADING_MEDIA);
await this.poapMomentsApi.uploadFile(
media.fileBinary,
url,
media.fileType,
onFileUploadProgress,
);
void onStepUpdate(CreateSteps.UPLOADING_MEDIA_METADATA);
// we will be adding metadata to the media in the future
void onStepUpdate(CreateSteps.PROCESSING_MEDIA);
try {
await this.poapMomentsApi.waitForMediaProcessing(key, timeOut);
} catch (error) {
void onStepUpdate(CreateSteps.PROCESSING_MEDIA_ERROR);
throw error;
}

return key;
}

async fetch({
limit,
offset,
Expand Down
7 changes: 3 additions & 4 deletions packages/moments/src/client/dtos/create/CreateInput.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { CreateSteps } from './CreateSteps';
import { CreateMedia } from './CreateMedia';

/**
* Interface representing the input needed to create a moment.
* @interface
* @property {number} dropId - The ID of the drop related to the moment.
* @property {number} [tokenId] - The ID of the token related to the moment (optional).
* @property {Uint8Array} fileBinary - The binary of the file to be uploaded.
* @property {string} author - The author of the moment. An Ethereum address.
* @property {string} description - The description of the moment (optional).
* @property {string} timeOut - The amount of time to wait until media is processed.
* @property {string} fileType - The type of the file.
* @property {(step: CreateSteps) => void | Promise<void>} [onStepUpdate] - Optional callback function to be called when the step changes.
* @property {(progress: number) => void | Promise<void>} [onFileProgress] - Optional callback function to be called when the file upload progress change - progress is a number between 0 and 1.
* @property {CreateMedia[]} medias - The media to be uploaded.
*/
export interface CreateMomentInput {
fileBinary: Uint8Array;
fileType: string;
author: string;
description?: string;
dropId: number;
tokenId?: number;
timeOut?: number;
onStepUpdate?: (step: CreateSteps) => void | Promise<void>;
onFileUploadProgress?: (progress: number) => void | Promise<void>;
medias?: CreateMedia[];
}
4 changes: 4 additions & 0 deletions packages/moments/src/client/dtos/create/CreateMedia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface CreateMedia {
fileBinary: Uint8Array;
fileType: string;
}
2 changes: 1 addition & 1 deletion packages/performance/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/performance",
"version": "0.0.35",
"version": "0.0.36",
"description": "Performance module for the poap.js library",
"type": "module",
"main": "dist/cjs/index.cjs",
Expand Down
6 changes: 3 additions & 3 deletions packages/poaps/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poaps",
"version": "0.0.35",
"version": "0.0.36",
"description": "Poaps module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,7 +26,7 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.0.35",
"@poap-xyz/utils": "0.0.35"
"@poap-xyz/providers": "0.0.36",
"@poap-xyz/utils": "0.0.36"
}
}
2 changes: 1 addition & 1 deletion packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/providers",
"version": "0.0.35",
"version": "0.0.36",
"description": "Providers module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/utils",
"version": "0.0.35",
"version": "0.0.36",
"description": "Utils module for the poap.js library",
"type": "module",
"main": "dist/cjs/index.cjs",
Expand All @@ -25,5 +25,5 @@
"scripts": {
"build": "rollup -c --bundleConfigAsCjs"
},
"stableVersion": "0.0.35"
"stableVersion": "0.0.36"
}
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -864,17 +864,17 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/drops@workspace:packages/drops"
dependencies:
"@poap-xyz/providers": 0.0.35
"@poap-xyz/utils": 0.0.35
"@poap-xyz/providers": 0.0.36
"@poap-xyz/utils": 0.0.36
languageName: unknown
linkType: soft

"@poap-xyz/moments@*, @poap-xyz/moments@workspace:packages/moments":
version: 0.0.0-use.local
resolution: "@poap-xyz/moments@workspace:packages/moments"
dependencies:
"@poap-xyz/providers": 0.0.35
"@poap-xyz/utils": 0.0.35
"@poap-xyz/providers": 0.0.36
"@poap-xyz/utils": 0.0.36
languageName: unknown
linkType: soft

Expand All @@ -888,12 +888,12 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/poaps@workspace:packages/poaps"
dependencies:
"@poap-xyz/providers": 0.0.35
"@poap-xyz/utils": 0.0.35
"@poap-xyz/providers": 0.0.36
"@poap-xyz/utils": 0.0.36
languageName: unknown
linkType: soft

"@poap-xyz/providers@*, @poap-xyz/[email protected].35, @poap-xyz/providers@workspace:packages/providers":
"@poap-xyz/providers@*, @poap-xyz/[email protected].36, @poap-xyz/providers@workspace:packages/providers":
version: 0.0.0-use.local
resolution: "@poap-xyz/providers@workspace:packages/providers"
dependencies:
Expand All @@ -904,7 +904,7 @@ __metadata:
languageName: unknown
linkType: soft

"@poap-xyz/utils@*, @poap-xyz/[email protected].35, @poap-xyz/utils@workspace:packages/utils":
"@poap-xyz/utils@*, @poap-xyz/[email protected].36, @poap-xyz/utils@workspace:packages/utils":
version: 0.0.0-use.local
resolution: "@poap-xyz/utils@workspace:packages/utils"
languageName: unknown
Expand Down

0 comments on commit 7c12674

Please sign in to comment.