Skip to content

Commit

Permalink
Add fetchFeed method
Browse files Browse the repository at this point in the history
Add fetchFeed method
  • Loading branch information
Shreyaschorge authored Nov 1, 2023
2 parents f1768d9 + a7e338d commit ae1f2cd
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neynar/nodejs-sdk",
"version": "0.5.2",
"version": "0.6.0",
"description": "SDK to interact with Neynar APIs (https://docs.neynar.com/)",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
43 changes: 43 additions & 0 deletions src/neynar-api/neynar-v2-api/neynar-api-v2-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
CastApiPostCastRequest,
CastWithInteractions,
SearchedUser,
FeedType,
FilterType,
} from "./openapi-farcaster";
import axios, { AxiosError, AxiosInstance } from "axios";
import { silentLogger, Logger } from "../common/logger";
Expand All @@ -33,6 +35,7 @@ export class NeynarV2APIClient {
public readonly apis: {
cast: CastApi;
reaction: ReactionApi;
feed: FeedApi;
};

/**
Expand Down Expand Up @@ -77,6 +80,7 @@ export class NeynarV2APIClient {
this.apis = {
cast: new CastApi(config, undefined, axiosInstance),
reaction: new ReactionApi(config, undefined, axiosInstance),
feed: new FeedApi(config, undefined, axiosInstance),
};
}

Expand Down Expand Up @@ -247,4 +251,43 @@ export class NeynarV2APIClient {
});
return response.data;
}

// ------------ Feed ------------

/**
* Get reverse chronological feed for a user based on their follow graph. See [Neynar documentation](https://docs.neynar.com/reference/get-feed)
*/
public async *fetchFeed(
fid: number,
options?: {
feedType?: FeedType;
filterType?: FilterType;
fids?: string;
parentUrl?: string;
pageSize?: number;
}
): AsyncGenerator<CastWithInteractions, void, undefined> {
let cursor: string | undefined;

while (true) {
const response = await this.apis.feed.feed({
feedType: options?.feedType,
filterType: options?.filterType,
fid: fid,
fids: options?.fids,
parentUrl: options?.parentUrl,
cursor: cursor,
limit: options?.pageSize,
});

// yield current page of casts
yield* response.data.casts;

// prep for next page
if (response.data.next.cursor === null) {
break;
}
cursor = response.data.next.cursor;
}
}
}
197 changes: 104 additions & 93 deletions src/neynar-api/neynar-v2-api/openapi-farcaster/apis/feed-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ export const FeedApiAxiosParamCreator = function (
/**
*
* @summary Retrieve casts based on filters
* @param {string} apiKey API key required for authentication.
* @param {number} fid (Optional) fid of user whose feed you want to create. By default, the API expects this field, except if you pass a filter_type
* @param {FeedFeedTypeEnum} [feedType] Defaults to following (requires fid or address). If set to filter (requires filter_type)
* @param {FeedFilterTypeEnum} [filterType] Used when feed_type&#x3D;filter. Can be set to fids (requires fids) or parent_url (requires parent_url)
* @param {FeedType} [feedType] Defaults to following (requires fid or address). If set to filter (requires filter_type)
* @param {FilterType} [filterType] Used when feed_type&#x3D;filter. Can be set to fids (requires fids) or parent_url (requires parent_url)
* @param {number} [fid] (Optional) fid of user whose feed you want to create. By default, the API expects this field, except if you pass a filter_type
* @param {string} [fids] Used when filter_type&#x3D;fids . Create a feed based on a list of fids. Max array size is 250. Requires feed_type and filter_type.
* @param {string} [parentUrl] Used when filter_type&#x3D;parent_url can be used to fetch content under any parent url e.g. FIP-2 channels on Warpcast. Requires feed_type and filter_type
* @param {string} [cursor] Pagination cursor.
Expand All @@ -64,20 +63,15 @@ export const FeedApiAxiosParamCreator = function (
* @throws {RequiredError}
*/
feed: async (
apiKey: string,
fid: number,
feedType?: FeedFeedTypeEnum,
filterType?: FeedFilterTypeEnum,
feedType?: FeedType,
filterType?: FilterType,
fid?: number,
fids?: string,
parentUrl?: string,
cursor?: string,
limit?: number,
options: AxiosRequestConfig = {}
): Promise<RequestArgs> => {
// verify required parameter 'apiKey' is not null or undefined
assertParamExists("feed", "apiKey", apiKey);
// verify required parameter 'fid' is not null or undefined
assertParamExists("feed", "fid", fid);
const localVarPath = `/farcaster/feed`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
Expand All @@ -94,6 +88,13 @@ export const FeedApiAxiosParamCreator = function (
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

// authentication ApiKeyAuth required
await setApiKeyToObject(
localVarHeaderParameter,
"api_key",
configuration
);

if (feedType !== undefined) {
localVarQueryParameter["feed_type"] = feedType;
}
Expand Down Expand Up @@ -122,10 +123,6 @@ export const FeedApiAxiosParamCreator = function (
localVarQueryParameter["limit"] = limit;
}

if (apiKey != null) {
localVarHeaderParameter["api_key"] = String(apiKey);
}

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions =
baseOptions && baseOptions.headers ? baseOptions.headers : {};
Expand Down Expand Up @@ -153,10 +150,9 @@ export const FeedApiFp = function (configuration?: Configuration) {
/**
*
* @summary Retrieve casts based on filters
* @param {string} apiKey API key required for authentication.
* @param {number} fid (Optional) fid of user whose feed you want to create. By default, the API expects this field, except if you pass a filter_type
* @param {FeedFeedTypeEnum} [feedType] Defaults to following (requires fid or address). If set to filter (requires filter_type)
* @param {FeedFilterTypeEnum} [filterType] Used when feed_type&#x3D;filter. Can be set to fids (requires fids) or parent_url (requires parent_url)
* @param {FeedType} [feedType] Defaults to following (requires fid or address). If set to filter (requires filter_type)
* @param {FilterType} [filterType] Used when feed_type&#x3D;filter. Can be set to fids (requires fids) or parent_url (requires parent_url)
* @param {number} [fid] (Optional) fid of user whose feed you want to create. By default, the API expects this field, except if you pass a filter_type
* @param {string} [fids] Used when filter_type&#x3D;fids . Create a feed based on a list of fids. Max array size is 250. Requires feed_type and filter_type.
* @param {string} [parentUrl] Used when filter_type&#x3D;parent_url can be used to fetch content under any parent url e.g. FIP-2 channels on Warpcast. Requires feed_type and filter_type
* @param {string} [cursor] Pagination cursor.
Expand All @@ -165,10 +161,9 @@ export const FeedApiFp = function (configuration?: Configuration) {
* @throws {RequiredError}
*/
async feed(
apiKey: string,
fid: number,
feedType?: FeedFeedTypeEnum,
filterType?: FeedFilterTypeEnum,
feedType?: FeedType,
filterType?: FilterType,
fid?: number,
fids?: string,
parentUrl?: string,
cursor?: string,
Expand All @@ -178,10 +173,9 @@ export const FeedApiFp = function (configuration?: Configuration) {
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<FeedResponse>
> {
const localVarAxiosArgs = await localVarAxiosParamCreator.feed(
apiKey,
fid,
feedType,
filterType,
fid,
fids,
parentUrl,
cursor,
Expand Down Expand Up @@ -212,45 +206,86 @@ export const FeedApiFactory = function (
/**
*
* @summary Retrieve casts based on filters
* @param {string} apiKey API key required for authentication.
* @param {number} fid (Optional) fid of user whose feed you want to create. By default, the API expects this field, except if you pass a filter_type
* @param {FeedFeedTypeEnum} [feedType] Defaults to following (requires fid or address). If set to filter (requires filter_type)
* @param {FeedFilterTypeEnum} [filterType] Used when feed_type&#x3D;filter. Can be set to fids (requires fids) or parent_url (requires parent_url)
* @param {string} [fids] Used when filter_type&#x3D;fids . Create a feed based on a list of fids. Max array size is 250. Requires feed_type and filter_type.
* @param {string} [parentUrl] Used when filter_type&#x3D;parent_url can be used to fetch content under any parent url e.g. FIP-2 channels on Warpcast. Requires feed_type and filter_type
* @param {string} [cursor] Pagination cursor.
* @param {number} [limit] Number of results to retrieve (default 25, max 150)
* @param {FeedApiFeedRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
feed(
apiKey: string,
fid: number,
feedType?: FeedFeedTypeEnum,
filterType?: FeedFilterTypeEnum,
fids?: string,
parentUrl?: string,
cursor?: string,
limit?: number,
options?: any
requestParameters: FeedApiFeedRequest = {},
options?: AxiosRequestConfig
): AxiosPromise<FeedResponse> {
return localVarFp
.feed(
apiKey,
fid,
feedType,
filterType,
fids,
parentUrl,
cursor,
limit,
requestParameters.feedType,
requestParameters.filterType,
requestParameters.fid,
requestParameters.fids,
requestParameters.parentUrl,
requestParameters.cursor,
requestParameters.limit,
options
)
.then((request) => request(axios, basePath));
},
};
};

/**
* Request parameters for feed operation in FeedApi.
* @export
* @interface FeedApiFeedRequest
*/
export interface FeedApiFeedRequest {
/**
* Defaults to following (requires fid or address). If set to filter (requires filter_type)
* @type {'filter' | 'following'}
* @memberof FeedApiFeed
*/
readonly feedType?: FeedType;

/**
* Used when feed_type&#x3D;filter. Can be set to fids (requires fids) or parent_url (requires parent_url)
* @type {'fids' | 'parent_url'}
* @memberof FeedApiFeed
*/
readonly filterType?: FilterType;

/**
* (Optional) fid of user whose feed you want to create. By default, the API expects this field, except if you pass a filter_type
* @type {number}
* @memberof FeedApiFeed
*/
readonly fid?: number;

/**
* Used when filter_type&#x3D;fids . Create a feed based on a list of fids. Max array size is 250. Requires feed_type and filter_type.
* @type {string}
* @memberof FeedApiFeed
*/
readonly fids?: string;

/**
* Used when filter_type&#x3D;parent_url can be used to fetch content under any parent url e.g. FIP-2 channels on Warpcast. Requires feed_type and filter_type
* @type {string}
* @memberof FeedApiFeed
*/
readonly parentUrl?: string;

/**
* Pagination cursor.
* @type {string}
* @memberof FeedApiFeed
*/
readonly cursor?: string;

/**
* Number of results to retrieve (default 25, max 150)
* @type {number}
* @memberof FeedApiFeed
*/
readonly limit?: number;
}

/**
* FeedApi - object-oriented interface
* @export
Expand All @@ -261,60 +296,36 @@ export class FeedApi extends BaseAPI {
/**
*
* @summary Retrieve casts based on filters
* @param {string} apiKey API key required for authentication.
* @param {number} fid (Optional) fid of user whose feed you want to create. By default, the API expects this field, except if you pass a filter_type
* @param {FeedFeedTypeEnum} [feedType] Defaults to following (requires fid or address). If set to filter (requires filter_type)
* @param {FeedFilterTypeEnum} [filterType] Used when feed_type&#x3D;filter. Can be set to fids (requires fids) or parent_url (requires parent_url)
* @param {string} [fids] Used when filter_type&#x3D;fids . Create a feed based on a list of fids. Max array size is 250. Requires feed_type and filter_type.
* @param {string} [parentUrl] Used when filter_type&#x3D;parent_url can be used to fetch content under any parent url e.g. FIP-2 channels on Warpcast. Requires feed_type and filter_type
* @param {string} [cursor] Pagination cursor.
* @param {number} [limit] Number of results to retrieve (default 25, max 150)
* @param {FeedApiFeedRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof FeedApi
*/
public feed(
apiKey: string,
fid: number,
feedType?: FeedFeedTypeEnum,
filterType?: FeedFilterTypeEnum,
fids?: string,
parentUrl?: string,
cursor?: string,
limit?: number,
requestParameters: FeedApiFeedRequest = {},
options?: AxiosRequestConfig
) {
return FeedApiFp(this.configuration)
.feed(
apiKey,
fid,
feedType,
filterType,
fids,
parentUrl,
cursor,
limit,
requestParameters.feedType,
requestParameters.filterType,
requestParameters.fid,
requestParameters.fids,
requestParameters.parentUrl,
requestParameters.cursor,
requestParameters.limit,
options
)
.then((request) => request(this.axios, this.basePath));
}
}

/**
* @export
*/
export const FeedFeedTypeEnum = {
Filter: "filter",
Following: "following",
} as const;
export type FeedFeedTypeEnum =
(typeof FeedFeedTypeEnum)[keyof typeof FeedFeedTypeEnum];
/**
* @export
*/
export const FeedFilterTypeEnum = {
Fids: "fids",
ParentUrl: "parent_url",
} as const;
export type FeedFilterTypeEnum =
(typeof FeedFilterTypeEnum)[keyof typeof FeedFilterTypeEnum];
export enum FeedType {
Filter = "filter",
Following = "following",
}

export enum FilterType {
Fids = "fids",
ParentUrl = "parent_url",
}

0 comments on commit ae1f2cd

Please sign in to comment.