-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdc-api.ts
More file actions
201 lines (188 loc) · 3.98 KB
/
pdc-api.ts
File metadata and controls
201 lines (188 loc) · 3.98 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { client } from './client';
import type { AccessTokenSet } from './oidc';
import type {
BaseField, ProposalBundle, ChangemakerBundle, SourceBundle, Source,
BaseFieldBundle,
} from '@pdc/sdk';
const callPdcApi = async <T>(
baseUrl: string,
path: string,
params: Record<string, string>,
method: 'get' | 'post',
token?: AccessTokenSet,
data?: unknown,
): Promise<T> => {
const url = new URL(path, baseUrl);
url.search = new URLSearchParams(params).toString();
const headers = token ? { authorization: `Bearer ${token.access_token}` } : {};
const response = await client.request<T>(
{
method,
url: url.toString(),
headers,
data,
},
);
return response.data;
};
const getBaseFields = (baseUrl: string, token: AccessTokenSet) => (
callPdcApi<BaseFieldBundle>(
baseUrl,
'/baseFields',
{
_page: '1',
_count: '2147483647',
},
'get',
token,
)
);
const getProposals = (baseUrl: string, token: AccessTokenSet) => (
callPdcApi<ProposalBundle>(
baseUrl,
'/proposals',
{
_page: '1',
_count: '1000',
},
'get',
token,
)
);
/**
* Get all (up to 10m) changemakers. Avoids authentication to get only direct attributes (shallow).
* The `fields` and `fiscalSponsors` (deep) attributes will be present but empty in this case.
*/
const getChangemakers = (baseUrl: string) => (
callPdcApi<ChangemakerBundle>(
baseUrl,
'/changemakers',
{
_page: '1',
_count: '10000000',
},
'get',
)
);
/**
* Get all (up to 1m) sources.
*/
const getSources = (baseUrl: string, token: AccessTokenSet) => (
callPdcApi<SourceBundle>(
baseUrl,
'/sources',
{
_page: '1',
_count: '1000000',
},
'get',
token,
)
);
/** A corrected WritableSource (the SDK's is a bit off as of this writing) */
export interface WritableSource {
label: string;
dataProviderShortCode: string;
}
const postSource = (baseUrl: string, token: AccessTokenSet, data: WritableSource) => (
callPdcApi<Source>(
baseUrl,
'/sources',
{},
'post',
token,
data,
)
);
// TODO: use the SDK, delete these temp types copied from the service repo
interface ChangemakerFieldValueBatch {
readonly id: number;
sourceId: number;
notes: string | null;
readonly createdAt: string;
readonly source: Source;
}
interface ChangemakerFieldValue {
readonly id: number;
changemakerId: number;
baseFieldShortCode: string;
batchId: number;
value: string;
readonly file: File | null;
goodAsOf: string | null;
readonly createdAt: string;
readonly baseField: BaseField;
readonly batch: ChangemakerFieldValueBatch;
readonly isValid: boolean;
}
interface WritableChangemakerFieldValueBatch {
sourceId: number;
notes: string | null;
}
interface WritableChangemakerFieldValue {
changemakerId: number;
baseFieldShortCode: string;
batchId: number;
value: string;
goodAsOf: string | null;
}
const postChangemakerFieldValueBatch = (
baseUrl: string,
token: AccessTokenSet,
data: WritableChangemakerFieldValueBatch,
) => (
callPdcApi<ChangemakerFieldValueBatch>(
baseUrl,
'/changemakerFieldValueBatches',
{},
'post',
token,
data,
)
);
const postChangemakerFieldValue = (
baseUrl: string,
token: AccessTokenSet,
data: WritableChangemakerFieldValue,
) => (
callPdcApi<ChangemakerFieldValue>(
baseUrl,
'/changemakerFieldValues',
{},
'post',
token,
data,
)
);
const postPlatformProviderData = (
baseUrl: string,
token: AccessTokenSet,
externalId: string,
platformProvider: string,
data: object,
) => (
callPdcApi(
baseUrl,
'/platformProviderResponses',
{},
'post',
token,
{
externalId,
platformProvider,
data,
},
)
);
export {
ChangemakerFieldValue,
ChangemakerFieldValueBatch,
getBaseFields,
getChangemakers,
getProposals,
getSources,
postChangemakerFieldValueBatch,
postChangemakerFieldValue,
postPlatformProviderData,
postSource,
};