-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·617 lines (541 loc) · 12.8 KB
/
index.js
File metadata and controls
executable file
·617 lines (541 loc) · 12.8 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/usr/bin/env node
const config = require('./config');
const {
getJson,
postJson,
getBuffer,
getText,
get,
} = require('./modules/http-client');
const store = { apiKey: '' };
const setApiKey = (apiKey) => {
store.apiKey = apiKey;
};
/**
* Helper: POST query to endpoint with token as query param, return JSON.
*/
const postWithToken = async (endpoint, query) => {
const url = endpoint + '?token=' + store.apiKey;
return postJson({ url, body: query });
};
/**
* Helper: GET endpoint with token as query param, return JSON.
*/
const getWithToken = async (url) => {
return getJson(url);
};
/*
* Query API
*/
const getFilingsQuery = async (query) => {
return postJson({
url: config.queryApi.endpoint,
body: query,
headers: { Authorization: store.apiKey },
});
};
/**
* Full-text Search API
*/
const getFilingsFullText = async (query) => {
return postJson({
url: config.fullTextApi.endpoint,
body: query,
headers: { Authorization: store.apiKey },
});
};
/**
* Download API
*/
const removeIxbrlRenderingQuery = (urlPath) => {
return urlPath.replace('/ix?doc=/', '/').replace('/ix.xhtml?doc=/', '/');
};
const edgarFileUrlToUrlPath = (edgarFileUrl) => {
return edgarFileUrl.replace(/.*\/edgar\/data\//, '/');
};
const addLeadingSlash = (urlPath) => {
if (urlPath.charAt(0) !== '/') {
return '/' + urlPath;
}
return urlPath;
};
const getFile = async (
edgarFileUrl,
params = {
decompress: true,
autoConvertToString: true,
},
) => {
const normalizedEdgarFileUrl = removeIxbrlRenderingQuery(edgarFileUrl);
let urlPath = edgarFileUrlToUrlPath(normalizedEdgarFileUrl);
urlPath = addLeadingSlash(urlPath);
const url =
config.downloadApiV2.endpoint + urlPath + '?token=' + store.apiKey;
const { data, headers } = await getBuffer(url);
if (!params.autoConvertToString) {
return data;
}
const contentType = headers['content-type'];
if (contentType && contentType.includes('text')) {
return data.toString('utf-8');
}
return data;
};
/**
* Render API
*/
const getFilingContent = async (url, type = 'html') => {
let _url;
if (type === 'pdf') {
_url = config.renderApi.endpoint + '&type=' + type + '&url=' + url;
} else {
const filename = url.replace(
'https://www.sec.gov/Archives/edgar/data/',
'',
);
_url = config.downloadApi.endpoint + filename + '?token=' + store.apiKey;
}
return getText(_url);
};
/**
* PDF Generator API
*/
const getPdf = async (url) => {
const fileUrl = url.replace(/ix\?doc=\//, '');
const requestUrl =
config.pdfGeneratorApi.endpoint +
'?type=pdf&url=' +
fileUrl +
'&token=' +
store.apiKey;
const { data } = await getBuffer(requestUrl);
return data;
};
/**
* XBRL-to-JSON converter and parser
*/
const xbrlToJson = async ({ htmUrl, xbrlUrl, accessionNo } = {}) => {
if (!htmUrl && !xbrlUrl && !accessionNo) {
throw new Error(
'Please provide one of the following arguments: htmUrl, xbrlUrl or accessionNo',
);
}
let requestUrl = config.xbrlToJsonApi.endpoint + '?token=' + store.apiKey;
if (htmUrl) {
requestUrl += '&htm-url=' + htmUrl;
}
if (xbrlUrl) {
requestUrl += '&xbrl-url=' + xbrlUrl;
}
if (accessionNo) {
requestUrl += '&accession-no=' + accessionNo;
}
return getJson(requestUrl);
};
/**
* Extractor API
*/
const getSection = async (filingUrl, section = '1A', returnType = 'text') => {
if (!filingUrl || !filingUrl.length) {
throw new Error('No valid filing URL provided');
}
const requestUrl =
config.extractorApi.endpoint +
`?token=${store.apiKey}&url=${filingUrl}&item=${section}&type=${returnType}`;
return get(requestUrl);
};
/**
* Mapping API
*/
const MAPPING_SUPPORTED_PARAMS = [
'cik',
'ticker',
'cusip',
'name',
'exchange',
'sector',
'industry',
];
const resolve = async (parameter, value) => {
if (!MAPPING_SUPPORTED_PARAMS.includes(parameter.toLowerCase())) {
throw new Error(
'Parameter not supported. Supported parameters: ' +
MAPPING_SUPPORTED_PARAMS.join(', '),
);
}
const url =
config.mappingApi.endpoint +
'/' +
parameter.toLowerCase() +
'/' +
value +
'?token=' +
store.apiKey;
return getWithToken(url);
};
/**
* Form ADV API
*/
const getAdvFirms = async (query) => {
return postWithToken(config.formAdvApi.endpoint + '/firm', query);
};
const getAdvIndividuals = async (query) => {
return postWithToken(config.formAdvApi.endpoint + '/individual', query);
};
const getAdvDirectOwners = async (crd) => {
const url =
config.formAdvApi.endpoint +
'/schedule-a-direct-owners/' +
crd +
'?token=' +
store.apiKey;
return getWithToken(url);
};
const getAdvIndirectOwners = async (crd) => {
const url =
config.formAdvApi.endpoint +
'/schedule-b-indirect-owners/' +
crd +
'?token=' +
store.apiKey;
return getWithToken(url);
};
const getAdvPrivateFunds = async (crd) => {
const url =
config.formAdvApi.endpoint +
'/schedule-d-7-b-1/' +
crd +
'?token=' +
store.apiKey;
return getWithToken(url);
};
const getAdvOtherBusinessNames = async (crd) => {
const url =
config.formAdvApi.endpoint +
'/schedule-d-1-b/' +
crd +
'?token=' +
store.apiKey;
return getWithToken(url);
};
const getAdvSeparatelyManagedAccounts = async (crd) => {
const url =
config.formAdvApi.endpoint +
'/schedule-d-5-k/' +
crd +
'?token=' +
store.apiKey;
return getWithToken(url);
};
const getAdvFinancialIndustryAffiliations = async (crd) => {
const url =
config.formAdvApi.endpoint +
'/schedule-d-7-a/' +
crd +
'?token=' +
store.apiKey;
return getWithToken(url);
};
const getAdvBrochures = async (crd) => {
const url =
config.formAdvApi.endpoint + '/brochures/' + crd + '?token=' + store.apiKey;
return getWithToken(url);
};
/**
* Executive Compensation API
*/
const getExecComp = async (parameter) => {
if (typeof parameter === 'string') {
const url =
config.execCompApi.endpoint +
'/' +
parameter.toUpperCase() +
'?token=' +
store.apiKey;
return getWithToken(url);
} else if (typeof parameter === 'object') {
return postWithToken(config.execCompApi.endpoint, parameter);
} else {
throw new Error(
'Invalid parameter. Provide a ticker string or a query object.',
);
}
};
/**
* Float API (Outstanding Shares & Public Float)
*/
const getFloat = async ({ ticker, cik } = {}) => {
if (!ticker && !cik) {
throw new Error('Please provide either a ticker or cik parameter.');
}
const searchTerm = ticker ? '&ticker=' + ticker : '&cik=' + cik;
const url = config.floatApi.endpoint + '?token=' + store.apiKey + searchTerm;
return getWithToken(url);
};
/**
* Form N-PX API
*/
const getNpxMetadata = async (query) => {
return postWithToken(config.formNpxApi.endpoint, query);
};
const getNpxVotingRecords = async (accessionNo) => {
const url =
config.formNpxApi.endpoint + '/' + accessionNo + '?token=' + store.apiKey;
return getWithToken(url);
};
/**
* Simple POST-based API wrappers
*/
const getInsiderTrading = async (query) => {
return postWithToken(config.insiderTradingApi.endpoint, query);
};
const getForm144 = async (query) => {
return postWithToken(config.form144Api.endpoint, query);
};
const getForm13FHoldings = async (query) => {
return postWithToken(config.form13FHoldingsApi.endpoint, query);
};
const getForm13FCoverPages = async (query) => {
return postWithToken(config.form13FCoverPagesApi.endpoint, query);
};
const getFormNport = async (query) => {
return postWithToken(config.formNportApi.endpoint, query);
};
const getForm13DG = async (query) => {
return postWithToken(config.form13DGApi.endpoint, query);
};
const getFormNcen = async (query) => {
return postWithToken(config.formNcenApi.endpoint, query);
};
const getFormS1424B4 = async (query) => {
return postWithToken(config.formS1424B4Api.endpoint, query);
};
const getFormD = async (query) => {
return postWithToken(config.formDApi.endpoint, query);
};
const getFormC = async (query) => {
return postWithToken(config.formCApi.endpoint, query);
};
const getRegASearch = async (query) => {
return postWithToken(config.regASearchApi.endpoint, query);
};
const getForm1A = async (query) => {
return postWithToken(config.form1AApi.endpoint, query);
};
const getForm1K = async (query) => {
return postWithToken(config.form1KApi.endpoint, query);
};
const getForm1Z = async (query) => {
return postWithToken(config.form1ZApi.endpoint, query);
};
const getForm8K = async (query) => {
return postWithToken(config.form8KApi.endpoint, query);
};
const getDirectorsAndBoardMembers = async (query) => {
return postWithToken(config.directorsBoardMembersApi.endpoint, query);
};
const getSubsidiaries = async (query) => {
return postWithToken(config.subsidiaryApi.endpoint, query);
};
const getSecEnforcementActions = async (query) => {
return postWithToken(config.secEnforcementActionsApi.endpoint, query);
};
const getSecLitigations = async (query) => {
return postWithToken(config.secLitigationsApi.endpoint, query);
};
const getSecAdminProceedings = async (query) => {
return postWithToken(config.secAdminProceedingsApi.endpoint, query);
};
const getAaer = async (query) => {
return postWithToken(config.aaerApi.endpoint, query);
};
const getSroFilings = async (query) => {
return postWithToken(config.sroApi.endpoint, query);
};
const getEdgarEntities = async (query) => {
return postWithToken(config.edgarEntitiesApi.endpoint, query);
};
const getAuditFees = async (query) => {
return postWithToken(config.auditFeesApi.endpoint, query);
};
const getIngestionLog = async (date) => {
const url =
config.edgarIndexIngestionLogApi.endpoint +
'/' +
date +
'?token=' +
store.apiKey;
return getWithToken(url);
};
/**
* Exports
*/
const modules = {
setApiKey,
queryApi: {
setApiKey,
getFilings: getFilingsQuery,
},
fullTextSearchApi: {
setApiKey,
getFilings: getFilingsFullText,
},
downloadApi: {
setApiKey,
getFile,
},
renderApi: {
setApiKey,
getFilingContent,
},
pdfGeneratorApi: {
setApiKey,
getPdf,
},
xbrlApi: {
setApiKey,
xbrlToJson,
},
extractorApi: {
setApiKey,
getSection,
},
mappingApi: {
setApiKey,
resolve,
},
formAdvApi: {
setApiKey,
getFirms: getAdvFirms,
getIndividuals: getAdvIndividuals,
getDirectOwners: getAdvDirectOwners,
getIndirectOwners: getAdvIndirectOwners,
getPrivateFunds: getAdvPrivateFunds,
getOtherBusinessNames: getAdvOtherBusinessNames,
getSeparatelyManagedAccounts: getAdvSeparatelyManagedAccounts,
getFinancialIndustryAffiliations: getAdvFinancialIndustryAffiliations,
getBrochures: getAdvBrochures,
},
insiderTradingApi: {
setApiKey,
getData: getInsiderTrading,
},
form144Api: {
setApiKey,
getData: getForm144,
},
form13FHoldingsApi: {
setApiKey,
getData: getForm13FHoldings,
},
form13FCoverPagesApi: {
setApiKey,
getData: getForm13FCoverPages,
},
formNportApi: {
setApiKey,
getData: getFormNport,
},
form13DGApi: {
setApiKey,
getData: getForm13DG,
},
formNcenApi: {
setApiKey,
getData: getFormNcen,
},
formNpxApi: {
setApiKey,
getMetadata: getNpxMetadata,
getVotingRecords: getNpxVotingRecords,
},
formS1424B4Api: {
setApiKey,
getData: getFormS1424B4,
},
formDApi: {
setApiKey,
getData: getFormD,
},
formCApi: {
setApiKey,
getData: getFormC,
},
regASearchApi: {
setApiKey,
getData: getRegASearch,
},
form1AApi: {
setApiKey,
getData: getForm1A,
},
form1KApi: {
setApiKey,
getData: getForm1K,
},
form1ZApi: {
setApiKey,
getData: getForm1Z,
},
form8KApi: {
setApiKey,
getData: getForm8K,
},
execCompApi: {
setApiKey,
getData: getExecComp,
},
directorsBoardMembersApi: {
setApiKey,
getData: getDirectorsAndBoardMembers,
},
floatApi: {
setApiKey,
getFloat,
},
subsidiaryApi: {
setApiKey,
getData: getSubsidiaries,
},
secEnforcementActionsApi: {
setApiKey,
getData: getSecEnforcementActions,
},
secLitigationsApi: {
setApiKey,
getData: getSecLitigations,
},
secAdminProceedingsApi: {
setApiKey,
getData: getSecAdminProceedings,
},
aaerApi: {
setApiKey,
getData: getAaer,
},
sroFilingsApi: {
setApiKey,
getData: getSroFilings,
},
edgarEntitiesApi: {
setApiKey,
getData: getEdgarEntities,
},
auditFeesApi: {
setApiKey,
getData: getAuditFees,
},
edgarIndexApi: {
setApiKey,
getIngestionLog,
},
};
module.exports = modules;
/**
* Command Line Execution
*/
if (require.main === module) {
console.log(
'sec-api npm package working. Please import the package and use the provided methods to interact with the API.',
);
}