Skip to content

Commit

Permalink
refactor: Adapt Moments package to POAP Moments API v2 (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
reobin authored Oct 21, 2024
1 parent a219dc4 commit b7864a2
Show file tree
Hide file tree
Showing 26 changed files with 91 additions and 294 deletions.
9 changes: 9 additions & 0 deletions docs/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@

- `createMoment` does not upload media beforehand anymore. Use `createMomentAndUploadMedia` instead.
- For compass requests, the fetch signal is not a parameter of the `PoapCompass.request` method anymore, it must be given through the new `options` parameter: `compass.request(url, { variables }, { signal })`.

## 0.7.0

- `dropId` has been changed to `dropIds` on the Moment type. It is now an array of 0 to multiple numerical IDs.
- This change is reflected in both the Moment creation input and the Moment response during creation or fetching.
- The CID is no longer a property of the Moment type.
- The token ID is no longer a property of the Moment type.
- `patchMoment` is no longer a method of the Moments client.
- It is no longer possible to order fetched Moments by token ID or drop ID.
1 change: 0 additions & 1 deletion docs/pages/packages/moments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
## Features

- Create a Moment attached to a Drop or a specific POAP
- Update a Moment
- Fetch multiple Moments
- Fetch a single Moment

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/packages/moments/FetchMoments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const paginatedMoments: PaginatedResult<Moment> = await client.fetch({
### By Drop

The list can be filtered by one or more Drops by giving `drop_ids` list with the
Drop IDs. Also, the list can be sorted by `id`, `created`, `tokenId` or `dropId`.
Drop IDs. Also, the list can be sorted by `id` or `created`.

```typescript
import { Moment } from '@poap-xyz/moments';
Expand Down
12 changes: 0 additions & 12 deletions docs/pages/packages/moments/UpdateMoment.mdx

This file was deleted.

9 changes: 2 additions & 7 deletions docs/pages/packages/moments/UploadMoments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ const mimeType = mime.getType('path/to/poap-moment.png');
// Upload it.
const moment: Moment = await client.createMomentAndUploadMedia({
/**
* Moments are associated with a Drop.
* Drops to be associated to the Moment.
*/
dropId: 110148,

/**
* The Token ID related to the moment (optional).
*/
tokenId: 6568008,
dropIds: [110148],

/**
* Who is uploading the moment.
Expand Down
3 changes: 1 addition & 2 deletions docs/pages/packages/moments/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"FetchMoments": "Fetch Moments",
"UploadMoments": "Upload Moments",
"UpdateMoment": "Update Moment"
"UploadMoments": "Upload Moments"
}
4 changes: 2 additions & 2 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.5.5",
"version": "0.5.6",
"description": "Drops module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down Expand Up @@ -29,7 +29,7 @@
"node": ">=18"
},
"dependencies": {
"@poap-xyz/providers": "0.6.0",
"@poap-xyz/providers": "0.7.0",
"@poap-xyz/utils": "0.5.5"
}
}
3 changes: 1 addition & 2 deletions packages/moments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ const client = new MomentsClient(
);

const input: CreateMomentInput = {
dropId: 110148,
tokenId: 6568008, // Optional: The Token ID related to the moment
dropIds: [110148],
media: [{
fileBinary: await fs.promises.readFile('src/assets/poap.png'),
fileType: 'image/png',
Expand Down
4 changes: 2 additions & 2 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.6.0",
"version": "0.7.0",
"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.5.5",
"@poap-xyz/providers": "0.7.0",
"@poap-xyz/utils": "0.5.5",
"uuid": "^9.0.0"
},
Expand Down
56 changes: 12 additions & 44 deletions packages/moments/src/client/MomentsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import { MomentsClient } from './MomentsClient';
import { CreateMomentInput } from './dtos/create/CreateInput';
import { CreateSteps } from './dtos/create/CreateSteps';
import { v4 } from 'uuid';
import { PatchMomentInput } from './dtos/patch/PatchInput';
import { CreateAndUploadMomentInput } from './dtos/create/CreateAndUploadInput';

describe('MomentsClient', () => {
const MOMENT_ID = 'this-is-a-moment-id';
const DROP_ID = 420;
const TOKEN_ID = 69;
const DROP_IDS = [420];
const AUTHOR = '0x7CE5368171cC3D988157d7dab3D313d7bd43de3e';
const FILE_1 = Buffer.from('This is the file 1');
const FILE_1_TYPE = 'image/png';
const FILE_2 = Buffer.from('This is the file 2');
const FILE_2_TYPE = 'image/jpeg';
const MEDIA_UPLOAD_URL = 'this-is-a-media-upload-url';
const DESCRIPTION = 'This is a description';
const MOMENT_CID =
'0001-7ce5368171cc3d988157d7dab3d313d7bd43de3e-365e5b83699adce0825021d011f1bf73bd5ef9369d06e49645afbea2ef34f54e0557c1d4742c8bd6d1f7a02be4aa483c03888af0aa143d5aa7351e2baaf931231c.moment';
const MEDIAS_TO_CREATE = [
{
fileBinary: FILE_1,
Expand Down Expand Up @@ -53,8 +49,7 @@ describe('MomentsClient', () => {
compassProviderMocked,
);
const inputs: CreateAndUploadMomentInput = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
dropIds: DROP_IDS,
media: MEDIAS_TO_CREATE,
author: AUTHOR,
onStepUpdate,
Expand All @@ -63,9 +58,9 @@ describe('MomentsClient', () => {
poapMomentsAPIMocked.createMoment.mockResolvedValue({
id: MOMENT_ID,
author: AUTHOR,
createdOn: new Date(),
dropId: DROP_ID,
tokenId: TOKEN_ID,
createdOn: new Date().toISOString(),
dropIds: DROP_IDS,
description: DESCRIPTION,
});
const mediaKeys: string[] = [];
poapMomentsAPIMocked.getSignedUrl.mockImplementation(async () => {
Expand All @@ -78,8 +73,7 @@ describe('MomentsClient', () => {
});

const EXPECTED_MOMENT_CREATE_INPUT = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
dropIds: DROP_IDS,
author: AUTHOR,
description: DESCRIPTION,
mediaKeys,
Expand All @@ -91,8 +85,7 @@ describe('MomentsClient', () => {
// THEN
expect(moment.id).toBe(MOMENT_ID);
expect(moment.author).toBe(AUTHOR);
expect(moment.dropId).toBe(DROP_ID);
expect(moment.tokenId).toBe(TOKEN_ID);
expect(moment.dropIds).toBe(DROP_IDS);
expect(poapMomentsAPIMocked.createMoment).toHaveBeenCalledWith(
EXPECTED_MOMENT_CREATE_INPUT,
);
Expand Down Expand Up @@ -124,8 +117,7 @@ describe('MomentsClient', () => {
compassProviderMocked,
);
const inputs: CreateMomentInput = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
dropIds: DROP_IDS,
mediaKeys: MEDIA_KEYS,
author: AUTHOR,
onStepUpdate,
Expand All @@ -134,14 +126,12 @@ describe('MomentsClient', () => {
poapMomentsAPIMocked.createMoment.mockResolvedValue({
id: MOMENT_ID,
author: AUTHOR,
createdOn: new Date(),
dropId: DROP_ID,
tokenId: TOKEN_ID,
createdOn: new Date().toISOString(),
dropIds: DROP_IDS,
});

const EXPECTED_MOMENT_CREATE_INPUT = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
dropIds: DROP_IDS,
author: AUTHOR,
description: DESCRIPTION,
mediaKeys: MEDIA_KEYS,
Expand All @@ -153,8 +143,7 @@ describe('MomentsClient', () => {
// THEN
expect(moment.id).toBe(MOMENT_ID);
expect(moment.author).toBe(AUTHOR);
expect(moment.dropId).toBe(DROP_ID);
expect(moment.tokenId).toBe(TOKEN_ID);
expect(moment.dropIds).toBe(DROP_IDS);
expect(poapMomentsAPIMocked.createMoment).toHaveBeenCalledWith(
EXPECTED_MOMENT_CREATE_INPUT,
);
Expand All @@ -164,25 +153,4 @@ describe('MomentsClient', () => {
expect(onStepUpdate).toHaveBeenCalledTimes(2);
});
});
describe('updateMoment', () => {
it('should update a moment', async () => {
// GIVEN
const client = new MomentsClient(
poapMomentsAPIMocked,
compassProviderMocked,
);
const input: PatchMomentInput = {
cid: MOMENT_CID,
};
poapMomentsAPIMocked.patchMoment.mockResolvedValue();

// WHEN
await client.patchMoment(MOMENT_ID, input);

// THEN
expect(poapMomentsAPIMocked.patchMoment).toHaveBeenCalledWith(MOMENT_ID, {
cid: MOMENT_CID,
});
});
});
});
20 changes: 1 addition & 19 deletions packages/moments/src/client/MomentsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { CreateMedia } from './dtos/create/CreateMedia';
import { CreateMomentInput } from './dtos/create/CreateInput';
import { CreateSteps } from './dtos/create/CreateSteps';
import { PatchMomentInput } from './dtos/patch/PatchInput';
import { FetchMomentsInput } from './dtos/fetch/FetchMomentsInput';
import { MomentsSortFields } from './dtos/fetch/MomentsSortFields';
import { CreateAndUploadMomentInput } from './dtos/create/CreateAndUploadInput';
Expand Down Expand Up @@ -57,9 +56,8 @@ export class MomentsClient {
public async createMoment(input: CreateMomentInput): Promise<Moment> {
void input.onStepUpdate?.(CreateSteps.UPLOADING_MOMENT);
const response = await this.poapMomentsApi.createMoment({
dropId: input.dropId,
dropIds: input.dropIds,
author: input.author,
tokenId: input.tokenId,
description: input.description,
mediaKeys: input.mediaKeys || [],
});
Expand Down Expand Up @@ -142,14 +140,11 @@ export class MomentsClient {
offset,
id,
createdOrder,
token_id,
drop_ids,
from,
to,
author,
idOrder,
tokenIdOrder,
dropIdOrder,
}: FetchMomentsInput): Promise<PaginatedResult<Moment>> {
const variables: MomentsQueryVariables = {
limit,
Expand All @@ -159,18 +154,9 @@ export class MomentsClient {
MomentsSortFields.StartDate,
createdOrder,
),
...createOrderBy<MomentsSortFields>(
MomentsSortFields.TokenId,
tokenIdOrder,
),
...createOrderBy<MomentsSortFields>(
MomentsSortFields.DropId,
dropIdOrder,
),
...createOrderBy<MomentsSortFields>(MomentsSortFields.Id, idOrder),
},
where: {
...createEqFilter('token_id', token_id),
...createInFilter('drop_id', drop_ids),
...createLikeFilter('author', author),
...createBetweenFilter('created_on', from, to),
Expand All @@ -194,8 +180,4 @@ export class MomentsClient {

return result;
}

public async patchMoment(id: string, input: PatchMomentInput): Promise<void> {
await this.poapMomentsApi.patchMoment(id, input);
}
}
23 changes: 9 additions & 14 deletions packages/moments/src/client/dtos/create/CreateAndUploadInput.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { CreateSteps } from './CreateSteps';
import { CreateMedia } from './CreateMedia';

/**
* Interface representing the input needed to create a moment and upload media in one action.
* @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 {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 {(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[]} media - The media to be uploaded.
*/
/** Interface representing the input needed to create a moment and upload media in one action. */
export interface CreateAndUploadMomentInput {
/** The author of the moment. An Ethereum address. */
author: string;
/** The description of the moment. */
description?: string;
dropId: number;
tokenId?: number;
/** The IDs of the drops to associate to the moment. */
dropIds: number[];
/** The amount of time to wait until media is processed. */
timeOut?: number;
/** Callback function to be called when the progress step changes. */
onStepUpdate?: (step: CreateSteps) => void | Promise<void>;
/** Callback function to be called when the file upload progress change - progress is a number between 0 and 1. */
onFileUploadProgress?: (progress: number) => void | Promise<void>;
/** The media to be uploaded and attached to the Moment. */
media?: CreateMedia[];
}
22 changes: 8 additions & 14 deletions packages/moments/src/client/dtos/create/CreateInput.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { CreateSteps } from './CreateSteps';

/**
* 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 {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 {(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 {string[]} mediaKeys - The media keys previously uploaded to attach to the Moment.
*/
/** Interface representing the input needed to create a moment. */
export interface CreateMomentInput {
/** The author of the moment. An Ethereum address. */
author: string;
/** The description of the moment. */
description?: string;
dropId: number;
tokenId?: number;
/** The IDs of the drops to associate to the moment. */
dropIds: number[];
/** The amount of time to wait until media is processed. */
timeOut?: number;
/** Callback function to be called when the step changes. */
onStepUpdate?: (step: CreateSteps) => void | Promise<void>;
/** The media keys previously uploaded to attach to the Moment. */
mediaKeys?: string[];
}
6 changes: 0 additions & 6 deletions packages/moments/src/client/dtos/fetch/FetchMomentsInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@ import { Order, PaginationInput } from '@poap-xyz/utils';
* @property {number} limit - The maximum number of moments to fetch.
* @property {number} offset - The number of moments to skip before starting to fetch.
* @property {string} [createdOrder] - The order to use for sorting by creation time (optional).
* @property {string} [tokenIdOrder] - The order to use for sorting by token ID (optional).
* @property {string} [dropIdOrder] - The order to use for sorting by drop ID (optional).
* @property {string} [idOrder] - The order to use for sorting by moment ID (optional).
* @property {string} [author] - The author of the moments to fetch (optional).
* @property {string} [from] - The start date for filtering moments by creation time (optional).
* @property {string} [to] - The end date for filtering moments by creation time (optional).
* @property {string} [id] - The moment ID to fetch (optional).
* @property {number} [token_id] - The token ID to filter moments by (optional).
* @property {number} [drop_id] - The drop ID to filter moments by (optional).
*/
export interface FetchMomentsInput extends PaginationInput {
createdOrder?: Order;
tokenIdOrder?: Order;
dropIdOrder?: Order;
idOrder?: Order;
author?: string;
from?: string;
to?: string;
id?: string;
token_id?: number;
drop_ids?: number[];
}
Loading

0 comments on commit b7864a2

Please sign in to comment.