Skip to content

Commit

Permalink
Merge branch 'main' into remove-examples-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jm42 authored Oct 7, 2024
2 parents 73d3414 + ebe90dc commit fb4f38d
Show file tree
Hide file tree
Showing 26 changed files with 180 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

jobs:
build_and_publish:
build_and_publish_production_release:
runs-on: ubuntu-latest

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/npm_publish_tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permissions:
actions: write

jobs:
build_and_publish:
build_and_publish_beta_tag:
runs-on: ubuntu-latest

steps:
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/packages/drops/CreateDrop.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Using `DropsClient` to [setup a Drop](https://poap.zendesk.com/hc/en-us/articles/9702718846989-How-do-I-set-up-a-POAP-Drop)
is straightforward, there is no need of being authenticated with an email and
the only requiriments are having the API key.
the only requirements are having the API key.

```typescript
import fsPromises from 'fs/promises';
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/packages/drops/FetchDrops.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fetch Drops

When retrieving Drops we have to paginated using `offset` and `limit`. But besides we can sort the
When retrieving Drops we have to paginate using `offset` and `limit`. But besides we can sort the
results and filter them by many options.

```typescript
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/packages/moments/UploadMoments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const fileBuffer = await fsPromises.readFile('path/to/poap-moment.png');
const mimeType = mime.getType('path/to/poap-moment.png');

// Upload it.
const moment: Moment = await client.createMoment({
const moment: Moment = await client.createMomentAndUploadMedia({
/**
* Moments are associated with a Drop.
*/
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/packages/poaps/EmailReservation.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Email Reservation

A POAP can be reserved with an email where to receive the instructions to mint
the it to the blockchain in the future. The email could be not be send in cases
that another email is send in their place or the collector knows already how to
it to the blockchain in the future. The email could not be sent in cases
that another email is sent in their place or the collector knows already how to
mint it afterwards.

```typescript
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/packages/poaps/Mint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const poap: POAP = (

## Sync

All the minting process can done automatically by `PoapsClient`.
All the minting processes can done automatically by `PoapsClient`.

```typescript
import { POAP } from '@poap-xyz/poaps';
Expand Down
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.5.3",
"version": "0.5.5",
"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.5.3",
"@poap-xyz/utils": "0.5.3"
"@poap-xyz/providers": "0.5.5",
"@poap-xyz/utils": "0.5.5"
}
}
15 changes: 7 additions & 8 deletions packages/drops/src/DropsClient.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { CompassProvider, DropApiProvider } from '@poap-xyz/providers';
import {
PaginatedResult,
nextCursor,
createBetweenFilter,
createInFilter,
Order,
isNumeric,
removeSpecialCharacters,
createOrderBy,
createBoolFilter,
createInFilter,
createLikeFilter,
createOrderBy,
isNumeric,
nextCursor,
Order,
PaginatedResult,
toPOAPDate,
} from '@poap-xyz/utils';
import { Drop } from './domain/Drop';
Expand Down Expand Up @@ -116,7 +115,7 @@ export class DropsClient {
offset,
...(isNumeric(search) && { orderBy: { id: Order.ASC } }),
args: {
search: removeSpecialCharacters(search),
search,
},
};

Expand Down
34 changes: 22 additions & 12 deletions packages/drops/src/domain/Drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ export class Drop {
emailReservationCount: number;

public static fromCompass(response: DropResponse): Drop {
const images: { crop: string; original: string } =
response.drop_image.gateways.reduce(
(images, gateway) => ({
...images,
[gateway.type.toLowerCase()]: gateway.url,
}),
{
crop: response.image_url,
original: response.image_url,
},
);
const images = Drop.getDropImageFromCompass(response);

return new Drop({
id: Number(response.id),
Expand Down Expand Up @@ -66,10 +56,30 @@ export class Drop {
transferCount: Number(
response.stats_by_chain_aggregate.aggregate.sum.transfer_count,
),
emailReservationCount: Number(response.email_claims_stats?.total) || 0,
emailReservationCount: Number(response.email_claims_stats?.reserved) || 0,
});
}

private static getDropImageFromCompass(response: DropResponse): {
crop: string;
original: string;
} {
const defaultImage = {
crop: response.image_url,
original: response.image_url,
};

return (
response.drop_image?.gateways.reduce(
(images, gateway) => ({
...images,
[gateway.type.toLowerCase()]: gateway.url,
}),
defaultImage,
) || defaultImage
);
}

public static fromProvider(response: ProviderDropResponse): Drop {
return new Drop({
id: response.id,
Expand Down
2 changes: 1 addition & 1 deletion packages/drops/src/queries/PaginatedDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const PAGINATED_DROPS_QUERY = `
}
}
email_claims_stats {
total
reserved
}
drop_image {
gateways {
Expand Down
2 changes: 1 addition & 1 deletion packages/drops/src/queries/SearchDrops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const SEARCH_DROPS_QUERY = `
}
}
email_claims_stats {
total
reserved
}
drop_image {
gateways {
Expand Down
4 changes: 2 additions & 2 deletions packages/drops/src/types/DropResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export interface DropResponse {
};
};
email_claims_stats: {
total: number;
reserved: number;
};
drop_image: {
gateways: Array<{
type: 'CROP' | 'ORIGINAL';
url: string;
}>;
};
} | null;
}
2 changes: 1 addition & 1 deletion packages/moments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const input: CreateMomentInput = {
},
timeOut: 5000, // Optional: Set a timeout for the media processing
};
const moment: Moment = await client.createMoment(input);
const moment: Moment = await client.createMomentAndUploadMedia(input);
```

Explanations for each step:
Expand Down
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.5.3",
"version": "0.6.0",
"description": "Moments module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,8 +26,8 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.5.3",
"@poap-xyz/utils": "0.5.3",
"@poap-xyz/providers": "0.5.5",
"@poap-xyz/utils": "0.5.5",
"uuid": "^9.0.0"
},
"engines": {
Expand Down
61 changes: 57 additions & 4 deletions packages/moments/src/client/MomentsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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';
Expand All @@ -29,6 +30,10 @@ describe('MomentsClient', () => {
fileType: FILE_2_TYPE,
},
];
const MEDIA_KEYS = [
'45201634-1996-4243-9c24-31da706be427',
'fbc3cf5f-fd65-4d2f-88fa-7025ebc7d631',
];

let poapMomentsAPIMocked: MockProxy<PoapMomentsApi>;
let compassProviderMocked: MockProxy<PoapCompass>;
Expand All @@ -40,14 +45,14 @@ describe('MomentsClient', () => {
onStepUpdate = jest.fn();
});

describe('createMoment', () => {
it('should create a moment', async () => {
describe('createMomentAndUploadMedia', () => {
it('should create a moment and upload media', async () => {
// GIVEN
const client = new MomentsClient(
poapMomentsAPIMocked,
compassProviderMocked,
);
const inputs: CreateMomentInput = {
const inputs: CreateAndUploadMomentInput = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
media: MEDIAS_TO_CREATE,
Expand Down Expand Up @@ -81,7 +86,7 @@ describe('MomentsClient', () => {
};

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

// THEN
expect(moment.id).toBe(MOMENT_ID);
Expand Down Expand Up @@ -111,6 +116,54 @@ describe('MomentsClient', () => {
expect(onStepUpdate).toHaveBeenCalledTimes(4);
});
});
describe('createMoment', () => {
it('should create a moment and attach media keys', async () => {
// GIVEN
const client = new MomentsClient(
poapMomentsAPIMocked,
compassProviderMocked,
);
const inputs: CreateMomentInput = {
dropId: DROP_ID,
tokenId: TOKEN_ID,
mediaKeys: MEDIA_KEYS,
author: AUTHOR,
onStepUpdate,
description: DESCRIPTION,
};
poapMomentsAPIMocked.createMoment.mockResolvedValue({
id: MOMENT_ID,
author: AUTHOR,
createdOn: new Date(),
dropId: DROP_ID,
tokenId: TOKEN_ID,
});

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

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

// 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(poapMomentsAPIMocked.createMoment).toHaveBeenCalledWith(
EXPECTED_MOMENT_CREATE_INPUT,
);
expect(poapMomentsAPIMocked.uploadFile).not.toHaveBeenCalledWith();
expect(onStepUpdate).toHaveBeenCalledWith(CreateSteps.UPLOADING_MOMENT);
expect(onStepUpdate).toHaveBeenCalledWith(CreateSteps.FINISHED);
expect(onStepUpdate).toHaveBeenCalledTimes(2);
});
});
describe('updateMoment', () => {
it('should update a moment', async () => {
// GIVEN
Expand Down
21 changes: 15 additions & 6 deletions packages/moments/src/client/MomentsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ 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';

export class MomentsClient {
constructor(
private poapMomentsApi: PoapMomentsApi,
private CompassProvider: CompassProvider,
) {}

public async createMoment(input: CreateMomentInput): Promise<Moment> {
/** Uploads media files first, then creates the Moment. */
public async createMomentAndUploadMedia(
input: CreateAndUploadMomentInput,
): Promise<Moment> {
let mediaKeys: string[] = [];
if (input.media && input.media.length > 0) {
mediaKeys = await this.uploadMedias(
mediaKeys = await this.uploadMediaList(
input.media,
input.onStepUpdate,
input.onFileUploadProgress,
Expand All @@ -39,28 +43,33 @@ export class MomentsClient {
}

if (mediaKeys.length > 0) {
await this.awaitForMediasProcessing(
await this.awaitForMediaProcessing(
mediaKeys,
input.onStepUpdate,
input.timeOut,
);
}

return this.createMoment({ ...input, mediaKeys });
}

/** Creates the Moment, attaching previously uploaded media when applicable. */
public async createMoment(input: CreateMomentInput): Promise<Moment> {
void input.onStepUpdate?.(CreateSteps.UPLOADING_MOMENT);
const response = await this.poapMomentsApi.createMoment({
dropId: input.dropId,
author: input.author,
tokenId: input.tokenId,
description: input.description,
mediaKeys,
mediaKeys: input.mediaKeys || [],
});
void input.onStepUpdate?.(CreateSteps.FINISHED);

return Moment.fromCreated(response);
}

// eslint-disable-next-line max-statements
private async uploadMedias(
public async uploadMediaList(
mediaArray: CreateMedia[],
onStepUpdate?: (step: CreateSteps) => void | Promise<void>,
onFileUploadProgress?: (progress: number) => void | Promise<void>,
Expand Down Expand Up @@ -89,7 +98,7 @@ export class MomentsClient {
return mediaKeys;
}

private async awaitForMediasProcessing(
public async awaitForMediaProcessing(
mediaKeys: string[],
onStepUpdate?: (step: CreateSteps) => void | Promise<void>,
timeOut?: number,
Expand Down
Loading

0 comments on commit fb4f38d

Please sign in to comment.