mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 13:33:25 +00:00
fetcher seralization
This commit is contained in:
@@ -19,14 +19,9 @@ Todos:
|
|||||||
- makeStandardFetcher()
|
- makeStandardFetcher()
|
||||||
- do all parameters get passed to real fetch as expected?
|
- do all parameters get passed to real fetch as expected?
|
||||||
- does serialisation work as expected? (formdata + json + string)
|
- does serialisation work as expected? (formdata + json + string)
|
||||||
- do baseUrl settings work?
|
|
||||||
- does json responses get automatically parsed?
|
- does json responses get automatically parsed?
|
||||||
- makeFullUrl()
|
|
||||||
- do slashes get converted correctly?
|
|
||||||
- running individual scrapers
|
- running individual scrapers
|
||||||
- finish fetchers:
|
- finish fetchers:
|
||||||
- make baseUrl param work
|
|
||||||
- proper serialization (with content-type headers) for standard fetcher
|
|
||||||
- automatically parse json
|
- automatically parse json
|
||||||
- error logging for failed scrapers
|
- error logging for failed scrapers
|
||||||
- add all real providers
|
- add all real providers
|
||||||
|
24
src/fetchers/body.ts
Normal file
24
src/fetchers/body.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import FormData = require('form-data');
|
||||||
|
|
||||||
|
import { FetcherOptions } from '@/fetchers/types';
|
||||||
|
|
||||||
|
export interface SeralizedBody {
|
||||||
|
headers: Record<string, string>;
|
||||||
|
body: FormData | URLSearchParams | string | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function serializeBody(body: FetcherOptions['body']): SeralizedBody {
|
||||||
|
if (body === undefined || typeof body === 'string' || body instanceof URLSearchParams || body instanceof FormData)
|
||||||
|
return {
|
||||||
|
headers: {},
|
||||||
|
body,
|
||||||
|
};
|
||||||
|
|
||||||
|
// serialize as JSON
|
||||||
|
return {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
};
|
||||||
|
}
|
@@ -1,5 +1,6 @@
|
|||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
|
import { serializeBody } from '@/fetchers/body';
|
||||||
import { makeFullUrl } from '@/fetchers/common';
|
import { makeFullUrl } from '@/fetchers/common';
|
||||||
import { Fetcher } from '@/fetchers/types';
|
import { Fetcher } from '@/fetchers/types';
|
||||||
|
|
||||||
@@ -7,9 +8,15 @@ export function makeStandardFetcher(f: typeof fetch): Fetcher {
|
|||||||
const normalFetch: Fetcher = (url, ops) => {
|
const normalFetch: Fetcher = (url, ops) => {
|
||||||
const fullUrl = makeFullUrl(url, ops);
|
const fullUrl = makeFullUrl(url, ops);
|
||||||
|
|
||||||
|
const seralizedBody = serializeBody(ops.body);
|
||||||
|
|
||||||
return f(fullUrl, {
|
return f(fullUrl, {
|
||||||
method: ops.method,
|
method: ops.method,
|
||||||
body: JSON.stringify(ops.body), // TODO content type headers + proper serialization
|
headers: {
|
||||||
|
...seralizedBody.headers,
|
||||||
|
...ops.headers,
|
||||||
|
},
|
||||||
|
body: seralizedBody.body,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -5,7 +5,7 @@ export type FetcherOptions = {
|
|||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
query?: Record<string, string>;
|
query?: Record<string, string>;
|
||||||
method?: 'GET' | 'POST';
|
method?: 'GET' | 'POST';
|
||||||
body?: Record<string, any> | string | FormData;
|
body?: Record<string, any> | string | FormData | URLSearchParams;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DefaultedFetcherOptions = {
|
export type DefaultedFetcherOptions = {
|
||||||
|
Reference in New Issue
Block a user