-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathswapi-ts.ts
More file actions
130 lines (121 loc) · 4.18 KB
/
swapi-ts.ts
File metadata and controls
130 lines (121 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Typescript version clientlib for a subset of data in https://swapi.dev/
*/
import fetch from 'node-fetch';
const SWAPI_URL = 'https://swapi.dev/api/';
export interface SWAPI_Planet {
readonly name: string;
readonly rotation_period: string;
readonly orbital_period: string;
readonly diameter: string;
readonly climate: string;
readonly gravity: string;
readonly terrain: string;
readonly surface_water: string;
readonly population: string;
readonly residents: readonly string[];
readonly films: readonly string[];
readonly created: string;
readonly edited: string;
readonly url: string;
}
export interface SWAPI_Person {
readonly name: string,
readonly height: string,
readonly mass: string,
readonly hair_color: string,
readonly skin_color: string,
readonly eye_color: string,
readonly birth_year: string,
readonly gender: string,
readonly homeworld: string,
readonly films: readonly string[],
readonly species: readonly string[],
readonly vehicles: readonly string[],
readonly starships: readonly string[],
readonly created: string,
readonly edited: string,
readonly url: string,
}
export interface SWAPI_Film {
readonly title: string;
readonly episode_id: number;
readonly opening_crawl: string;
readonly director: string;
readonly producer: string;
readonly release_date: string;
readonly species: readonly string[];
readonly starships: readonly string[];
readonly vehicles: readonly string[];
readonly characters: readonly string[];
readonly planets: readonly string[];
readonly url: string;
readonly created: string;
readonly edited: string;
}
export interface SWAPI_Film_V2 {
readonly properties: ReadonlyArray<{
id: number;
title?: string;
episode_id?: number;
director?: string;
producer?: string;
}>;
}
export interface SWAPI_Vehicle {
readonly name: string;
readonly key: string;
}
interface SWAPI_Root {
readonly people: string;
readonly planets: string;
readonly films: string;
readonly species: string;
readonly vehicles: string;
readonly starships: string;
}
export interface SWAPIClientlibTypes {
getPlanets: (params: { readonly planet_ids: readonly number[] }) => Promise<readonly SWAPI_Planet[]>;
getPeople: (params: { readonly people_ids: readonly number[] }) => Promise<readonly SWAPI_Person[]>;
getVehicles: (params: { readonly vehicle_ids: readonly number[] }) => Promise<readonly SWAPI_Vehicle[]>;
getFilms: (params: { film_ids: Set<number> }) => Promise<readonly SWAPI_Film[]>;
getFilmsV2: (params: {
readonly film_ids: readonly number[];
readonly properties: readonly string[];
}) => Promise<SWAPI_Film_V2>;
getRoot: (params: {}) => Promise<SWAPI_Root>;
}
export default function (): SWAPIClientlibTypes {
return {
getPlanets: ({ planet_ids: planetIds }) =>
Promise.all(
planetIds.map((id) => fetch(new URL(`planets/${id}`, SWAPI_URL)).then((res) => res.json())),
),
getPeople: ({ people_ids: peopleIds }) =>
Promise.all(
peopleIds.map((id) => fetch(new URL(`people/${id}`, SWAPI_URL)).then((res) => res.json())),
),
getVehicles: ({ vehicle_ids: vehicleIds }) =>
Promise.all(
vehicleIds.map((id) => fetch(new URL(`vehicles/${id}`, SWAPI_URL)).then((res) => res.json())),
),
getFilms: ({ film_ids: filmIds }) =>
Promise.all(
[...filmIds].map((id) => fetch(new URL(`films/${id}`, SWAPI_URL)).then((res) => res.json())),
),
getFilmsV2: ({ film_ids: filmIds, properties }) => {
return Promise.resolve({
properties: [
{
id: 4,
director: 'George Lucas',
producer: 'Rick McCallum',
episode_id: 1,
title: 'The Phantom Menace',
},
],
});
},
getRoot: ({ }) => fetch(SWAPI_URL).then((res) => res.json()),
};
};