diff --git a/src/createFetchWrapper.ts b/src/createFetchWrapper.ts index b124a6d..b3c58fd 100644 --- a/src/createFetchWrapper.ts +++ b/src/createFetchWrapper.ts @@ -15,7 +15,6 @@ export default function createFetchWrapper(apiKey: string, type: AllowedTypes) { headers: { Accept: "application/json", "Content-Type": "application/json", - "User-Agent": "Pexels/JavaScript", Authorization: apiKey, }, }; @@ -24,12 +23,19 @@ export default function createFetchWrapper(apiKey: string, type: AllowedTypes) { return (path: string, params?: T) => fetch(`${baseUrl}${path}?${stringifyParams(params || {})}`, options).then( - (response) => { + async (response) => { if (!response.ok) { throw new Error(response.statusText); } - return response.json(); + const json = await response.json(); + + Object.defineProperty(json, "headers", { + value: response.headers, + enumerable: false, + }); + + return json; } ); } diff --git a/src/generateCollectionEndpoints.ts b/src/generateCollectionEndpoints.ts index 64e0865..de68817 100644 --- a/src/generateCollectionEndpoints.ts +++ b/src/generateCollectionEndpoints.ts @@ -1,26 +1,38 @@ import createFetchWrapper from "./createFetchWrapper"; -import { Collection, PaginationParams, ErrorResponse, Medium } from "./types"; +import { + Collection, + PaginationParams, + ErrorResponse, + Medium, + WithHeaders, +} from "./types"; -type AllReturn = +type AllReturn = ( | { page: number; per_page: number; collections: Collection[]; } - | ErrorResponse; + | ErrorResponse +) & + WithHeaders; -type MediaReturn = +type MediaReturn = ( | { page: number; per_page: number; total_results: number; media: (Medium & { type: "Video" | "Photo" })[]; } - | ErrorResponse; + | ErrorResponse +) & + WithHeaders; -type FeaturedReturn = +type FeaturedReturn = ( | { page: number; per_page: number; collections: Collection[] } - | ErrorResponse; + | ErrorResponse +) & + WithHeaders; export default function generateCollectionEndpoints(apiKey: string) { const fetchWrapper = createFetchWrapper(apiKey, "collections"); diff --git a/src/generatePhotoEndpoints.ts b/src/generatePhotoEndpoints.ts index 0fe22d5..2a4022e 100644 --- a/src/generatePhotoEndpoints.ts +++ b/src/generatePhotoEndpoints.ts @@ -2,23 +2,25 @@ import createFetchWrapper from "./createFetchWrapper"; import { Photo, PaginationParams, + PhotoFilterParams, ErrorResponse, Photos, PhotosWithTotalResults, + WithHeaders, } from "./types"; import { isPhotos } from "./typeCheckers"; -type SearchReturn = PhotosWithTotalResults | ErrorResponse; -type CuratedReturn = Photos | ErrorResponse; -type ShowReturn = Photo | ErrorResponse; -type RandomReturn = Photo | ErrorResponse; +type SearchReturn = (PhotosWithTotalResults | ErrorResponse) & WithHeaders; +type CuratedReturn = (Photos | ErrorResponse) & WithHeaders; +type ShowReturn = (Photo | ErrorResponse) & WithHeaders; +type RandomReturn = (Photo | ErrorResponse) & WithHeaders; export default function generatePhotoEndpoints(apiKey: string) { const fetchWrapper = createFetchWrapper(apiKey, "photo"); return { search( - params: PaginationParams & { query: string } + params: PaginationParams & PhotoFilterParams & { query: string } ): Promise { return fetchWrapper(`/search`, params); }, @@ -33,10 +35,17 @@ export default function generatePhotoEndpoints(apiKey: string) { const response = await this.curated({ page: randomPage, per_page: 1 }); if (isPhotos(response)) { - return response.photos[0] as RandomReturn; + const photo = response.photos[0]; + + Object.defineProperty(photo, "headers", { + value: response.headers, + enumerable: false, + }); + + return photo as RandomReturn; } - return response as ErrorResponse; + return response as RandomReturn; }, }; } diff --git a/src/generateVideoEndpoints.ts b/src/generateVideoEndpoints.ts index f842e4f..45db30e 100644 --- a/src/generateVideoEndpoints.ts +++ b/src/generateVideoEndpoints.ts @@ -5,11 +5,12 @@ import { Video, Videos, VideoFilterParams, + WithHeaders, } from "./types"; -type SearchReturn = Videos | ErrorResponse; -type PopularReturn = Videos | ErrorResponse; -type ShowReturn = Video; +type SearchReturn = (Videos | ErrorResponse) & WithHeaders; +type PopularReturn = (Videos | ErrorResponse) & WithHeaders; +type ShowReturn = Video & WithHeaders; export default function generatePhotoEndpoints(apiKey: string) { const fetchWrapper = createFetchWrapper(apiKey, "video"); diff --git a/src/types.ts b/src/types.ts index 013c7a8..cdb56a2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,6 +2,10 @@ export interface ErrorResponse { error: string; } +export interface WithHeaders { + headers: Headers; +} + export interface Params { [key: string]: string | number | undefined; } @@ -11,6 +15,13 @@ export interface PaginationParams extends Params { page?: number; } +export interface PhotoFilterParams extends Params { + orientation?: "landscape" | "portrait" | "square"; + size?: "large" | "medium" | "small"; + color?: string; + locale?: string; +} + export interface VideoFilterParams extends Params { min_width?: number; max_width?: number; @@ -18,7 +29,7 @@ export interface VideoFilterParams extends Params { max_duration?: number; } -interface PaginationObject { +export interface PaginationObject { url?: string; page: number; per_page: number;