Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/api-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export type ApiRequestInfo<EndpointUrl> = EndpointFields<EndpointUrl> & {
* Converts the request back into a URL to be sent to the Twitter API.
*/
toRequestUrl(): string;

/**
* Converts the request into a JSON body for POST requests.
*/
toRequestBody(): Record<string, unknown>;
};

/** Wrapper class for API request information. */
Expand Down Expand Up @@ -118,6 +123,17 @@ class ApiRequest<EndpointUrl> {

return `${this.url}?${params.toString()}`;
}

/**
* Converts the request into a JSON body for POST requests.
*/
toRequestBody(): Record<string, unknown> {
const body: Record<string, unknown> = {};
if (this.variables) body.variables = this.variables;
if (this.features) body.features = this.features;
if (this.fieldToggles) body.fieldToggles = this.fieldToggles;
return body;
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type RequestApiResult<T> =
* @param platform - The platform extensions to use.
* @param headers - The headers to include in the request.
* @param bearerTokenOverride - Optional bearer token to use instead of the default one.
* @param body - Optional JSON body to send with POST requests.
*/
export async function requestApi<T>(
url: string,
Expand All @@ -69,6 +70,7 @@ export async function requestApi<T>(
platform: PlatformExtensions = new Platform(),
headers: Headers = new Headers(),
bearerTokenOverride?: string,
body?: Record<string, unknown>,
): Promise<RequestApiResult<T>> {
log(`Making ${method} request to ${url}`);

Expand All @@ -87,6 +89,10 @@ export async function requestApi<T>(
headers.set('x-client-transaction-id', transactionId);
}

if (body && method === 'POST') {
headers.set('content-type', 'application/json');
}

let res: Response;
do {
const fetchParameters: FetchParameters = [
Expand All @@ -95,6 +101,7 @@ export async function requestApi<T>(
method,
headers,
credentials: 'include',
...(body && method === 'POST' ? { body: JSON.stringify(body) } : {}),
},
];

Expand Down
5 changes: 3 additions & 2 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,13 @@ async function getSearchTimeline(
}

const res = await requestApi<SearchTimeline>(
searchTimelineRequest.toRequestUrl(),
searchTimelineRequest.url,
auth,
'GET',
'POST',
undefined,
undefined,
bearerToken2,
searchTimelineRequest.toRequestBody(),
);

if (!res.success) {
Expand Down
Loading
Loading