Skip to content

Commit

Permalink
fix: rewrite api client to mask some server-side changes to the API
Browse files Browse the repository at this point in the history
fix #392
  • Loading branch information
gsgalloway committed Dec 5, 2022
1 parent 6870c57 commit f7f38d6
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"branches": [
{
"name": "4.0.x",
"range": "4.0.x"
},
"+([0-9])?(.{+([0-9]),x}).x",
"main",
"master",
Expand Down
4 changes: 2 additions & 2 deletions docs/classes/index.MerkleAPIClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ ___

### fetchMentionAndReplyNotifications

**fetchMentionAndReplyNotifications**(`__namedParameters?`): `AsyncGenerator`<`Notification`, `void`, `undefined`\>
**fetchMentionAndReplyNotifications**(`__namedParameters?`): `AsyncGenerator`<[`Notification`](../modules/merkleAPI_swagger.md#notification), `void`, `undefined`\>

#### Parameters

Expand All @@ -255,7 +255,7 @@ ___

#### Returns

`AsyncGenerator`<`Notification`, `void`, `undefined`\>
`AsyncGenerator`<[`Notification`](../modules/merkleAPI_swagger.md#notification), `void`, `undefined`\>

#### Defined in

Expand Down
18 changes: 9 additions & 9 deletions docs/classes/merkleAPI_swagger.CastsApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1209](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1209)
[src/merkleAPI/swagger/apis/casts-api.ts:1215](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1215)

___

Expand Down Expand Up @@ -161,7 +161,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1227](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1227)
[src/merkleAPI/swagger/apis/casts-api.ts:1233](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1233)

___

Expand Down Expand Up @@ -191,7 +191,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1245](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1245)
[src/merkleAPI/swagger/apis/casts-api.ts:1251](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1251)

___

Expand Down Expand Up @@ -222,7 +222,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1263](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1263)
[src/merkleAPI/swagger/apis/casts-api.ts:1275](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1275)

___

Expand Down Expand Up @@ -252,7 +252,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1281](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1281)
[src/merkleAPI/swagger/apis/casts-api.ts:1293](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1293)

___

Expand Down Expand Up @@ -285,7 +285,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1300](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1300)
[src/merkleAPI/swagger/apis/casts-api.ts:1312](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1312)

___

Expand Down Expand Up @@ -315,7 +315,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1327](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1327)
[src/merkleAPI/swagger/apis/casts-api.ts:1339](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1339)

___

Expand Down Expand Up @@ -345,7 +345,7 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1344](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1344)
[src/merkleAPI/swagger/apis/casts-api.ts:1356](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1356)

___

Expand Down Expand Up @@ -375,4 +375,4 @@ CastsApi

#### Defined in

[src/merkleAPI/swagger/apis/casts-api.ts:1361](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1361)
[src/merkleAPI/swagger/apis/casts-api.ts:1373](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/apis/casts-api.ts#L1373)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ InlineResponse20014Result

### notifications

**notifications**: `Notification`[]
**notifications**: [`Notification`](../modules/merkleAPI_swagger.md#notification)[]

**`Memberof`**

Expand Down
17 changes: 14 additions & 3 deletions docs/interfaces/merkleAPI_swagger.InlineResponse2008Result.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@ InlineResponse2008Result

### Properties

- [like](merkleAPI_swagger.InlineResponse2008Result.md#like)
- [reaction](merkleAPI_swagger.InlineResponse2008Result.md#reaction)

## Properties

### like

**like**: [`CastReaction`](merkleAPI_swagger.CastReaction.md)

#### Defined in

[src/merkleAPI/swagger/models/inline-response2008-result.ts:27](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/models/inline-response2008-result.ts#L27)

___

### reaction

**reaction**: [`CastReaction`](merkleAPI_swagger.CastReaction.md)

**`Memberof`**

InlineResponse2008Result
Note that the server's response will be written to the key `like` and not
`reaction`. MerkleAPIClient will attempt to copy the value of `like`
into `reaction` for backward compatibility.

#### Defined in

Expand Down
11 changes: 11 additions & 0 deletions docs/modules/merkleAPI_swagger.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
- [CastReactionType](merkleAPI_swagger.md#castreactiontype)
- [InlineResponse2006Result](merkleAPI_swagger.md#inlineresponse2006result)
- [InlineResponse2009Result](merkleAPI_swagger.md#inlineresponse2009result)
- [Notification](merkleAPI_swagger.md#notification)

### Variables

Expand Down Expand Up @@ -253,6 +254,16 @@ InlineResponse2009Result

[src/merkleAPI/swagger/models/inline-response2009-result.ts:20](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/models/inline-response2009-result.ts#L20)

___

### Notification

Ƭ **Notification**: [`NotificationCastMention`](../interfaces/merkleAPI_swagger.NotificationCastMention.md) \| [`NotificationCastReaction`](../interfaces/merkleAPI_swagger.NotificationCastReaction.md) \| [`NotificationCastReply`](../interfaces/merkleAPI_swagger.NotificationCastReply.md) \| [`NotificationFollow`](../interfaces/merkleAPI_swagger.NotificationFollow.md) \| [`NotificationRecast`](../interfaces/merkleAPI_swagger.NotificationRecast.md) \| [`NotificationWatchedCastReply`](../interfaces/merkleAPI_swagger.NotificationWatchedCastReply.md)

#### Defined in

[src/merkleAPI/swagger/models/Notification.ts:8](https://github.com/standard-crypto/farcaster-js/blob/main/src/merkleAPI/swagger/models/Notification.ts#L8)

## Variables

### BASE\_PATH
Expand Down
17 changes: 16 additions & 1 deletion src/merkleAPI/MerkleAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class MerkleAPIClient {
// yield current page of casts
for (const cast of response.data.result.casts) {
if (includeRecasts || cast.author.fid === user.fid) {
yield cast;
yield _rewritePublishedAt(cast);
}
}

Expand Down Expand Up @@ -619,3 +619,18 @@ function _isAuthTokenExpired(authToken: AuthToken): boolean {
const expires = new Date(authToken.expiresAt);
return now + THIRTY_SECONDS_IN_MILLIS > expires.valueOf();
}

/**
* Edits Cast objects to include a `publishedAt` field which was renamed
* server-side to `timestamp`. This is done solely for backward-compatibility.
*/
function _rewritePublishedAt(cast: Cast): Cast {
if (cast.ancestors?.casts !== undefined) {
cast.ancestors.casts = cast.ancestors.casts.map(_rewritePublishedAt);
}
if (cast.replies.casts !== undefined) {
cast.replies.casts = cast.replies.casts.map(_rewritePublishedAt);
}
cast.publishedAt = (cast as unknown as { timestamp: number }).timestamp;
return cast;
}
22 changes: 17 additions & 5 deletions src/merkleAPI/swagger/apis/casts-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const CastsApiAxiosParamCreator = function (
"Required parameter authorization was null or undefined when calling v2CastReactionsDelete."
);
}
const localVarPath = `/v2/cast-reactions`;
const localVarPath = `/v2/cast-likes`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, "https://example.com");
let baseOptions;
Expand Down Expand Up @@ -143,7 +143,7 @@ export const CastsApiAxiosParamCreator = function (
"Required parameter authorization was null or undefined when calling v2CastReactionsGet."
);
}
const localVarPath = `/v2/cast-reactions`;
const localVarPath = `/v2/cast-likes`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, "https://example.com");
let baseOptions;
Expand Down Expand Up @@ -211,7 +211,7 @@ export const CastsApiAxiosParamCreator = function (
"Required parameter authorization was null or undefined when calling v2CastReactionsPut."
);
}
const localVarPath = `/v2/cast-reactions`;
const localVarPath = `/v2/cast-likes`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, "https://example.com");
let baseOptions;
Expand Down Expand Up @@ -1076,7 +1076,13 @@ export const CastsApiFactory = function (
): Promise<AxiosResponse<InlineResponse2008>> {
return CastsApiFp(configuration)
.v2CastReactionsPut(authorization, body, options)
.then((request) => request(axios, basePath));
.then((request) => request(axios, basePath))
.then((response) => {
if (response.status === 200) {
response.data.result.reaction = response.data.result.like;
}
return response;
});
},
/**
* Get all users who recasted a cast.
Expand Down Expand Up @@ -1249,7 +1255,13 @@ export class CastsApi extends BaseAPI {
): Promise<AxiosResponse<InlineResponse2008>> {
return CastsApiFp(this.configuration)
.v2CastReactionsPut(authorization, body, options)
.then((request) => request(this.axios, this.basePath));
.then((request) => request(this.axios, this.basePath))
.then((response) => {
if (response.status === 200) {
response.data.result.reaction = response.data.result.like;
}
return response;
});
}
/**
* Get all users who recasted a cast.
Expand Down
1 change: 1 addition & 0 deletions src/merkleAPI/swagger/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export * from "./NotificationRecast";
export * from "./NotificationRecastContent";
export * from "./NotificationWatchedCastReply";
export * from "./NotificationWatchedCastReplyContent";
export * from "./Notification";
export * from "./CastReactionNotificationGroup";
export * from "./CastMentionNotificationGroup";
export * from "./CastReplyNotificationGroup";
Expand Down
7 changes: 4 additions & 3 deletions src/merkleAPI/swagger/models/inline-response2008-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import { CastReaction } from "./CastReaction";
*/
export interface InlineResponse2008Result {
/**
*
* @type {CastReaction}
* @memberof InlineResponse2008Result
* Note that the server's response will be written to the key `like` and not
* `reaction`. MerkleAPIClient will attempt to copy the value of `like`
* into `reaction` for backward compatibility.
*/
reaction: CastReaction;
like: CastReaction;
}
60 changes: 30 additions & 30 deletions tests/merkleAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ if (privateKey !== undefined && privateKey !== "") {
})) {
foundCast = true;
expect(cast.author.fid).to.eq(userGaviFid);
expectDefined(cast.publishedAt);
break;
}
expect(foundCast).to.be.true;
Expand Down Expand Up @@ -282,43 +283,42 @@ if (privateKey !== undefined && privateKey !== "") {
}
expect(assetFound).to.be.true;
});
});

describe("#fetchCustodyAddress", function () {
it("can lookup by fid", async function () {
const expectedCustodyAddr =
"0x74232bf61e994655592747e20bdf6fa9b9476f79";
const custodyAddr = await client.fetchCustodyAddressForUser("dwr");
expect(custodyAddr).to.eq(expectedCustodyAddr);
});
describe("#fetchCustodyAddress", function () {
it("can lookup by fid", async function () {
const expectedCustodyAddr =
"0x74232bf61e994655592747e20bdf6fa9b9476f79";
const custodyAddr = await client.fetchCustodyAddressForUser("dwr");
expect(custodyAddr).to.eq(expectedCustodyAddr);
});
});

describe("auth", function () {
let token: AuthToken;
it("can create an auth token", async function () {
token = await client.createAuthToken();
expectDefined(token);
});
it("can revoke an auth token", async function () {
expectDefined(token);
await client.revokeAuthToken(token);
});
describe("auth", function () {
let token: AuthToken;
it("can create an auth token", async function () {
token = await client.createAuthToken();
expectDefined(token);
});
it("can revoke an auth token", async function () {
expectDefined(token);
await client.revokeAuthToken(token);
});
});

describe("notifications", function () {
it("can fetch mention and reply notifications", async function () {
const notifications =
await client.fetchMentionAndReplyNotifications();
describe("notifications", function () {
it("can fetch mention and reply notifications", async function () {
const notifications = await client.fetchMentionAndReplyNotifications();

let notificationFound = false;
// eslint-disable-next-line no-unreachable-loop
for await (const notification of notifications) {
expectDefined(notification);
notificationFound = true;
break;
}
let notificationFound = false;
// eslint-disable-next-line no-unreachable-loop
for await (const notification of notifications) {
expectDefined(notification);
notificationFound = true;
break;
}

expect(notificationFound).to.be.true;
});
expect(notificationFound).to.be.true;
});
});
});
Expand Down

0 comments on commit f7f38d6

Please sign in to comment.