|
| 1 | +import { BaseAdapter } from '../base.adapter'; |
| 2 | +import { |
| 3 | + type ProviderRequest, |
| 4 | + type ProviderRawResponse, |
| 5 | + ProviderErrorCode, |
| 6 | +} from '../../types/provider'; |
| 7 | +import type { FigiInstrument, FigiMapOutput, FigiSearchOutput, FigiFilterOutput } from './types'; |
| 8 | + |
| 9 | +const FIGI_BASE = 'https://api.openfigi.com/v3'; |
| 10 | + |
| 11 | +/** |
| 12 | + * OpenFIGI adapter (UC-357). |
| 13 | + * |
| 14 | + * Bloomberg Financial Instrument Global Identifier — ISO 18774. |
| 15 | + * 300M+ instruments, 45K+ exchanges. POST-based API. |
| 16 | + * Optional API key increases rate limits. |
| 17 | + */ |
| 18 | +export class FigiAdapter extends BaseAdapter { |
| 19 | + private readonly apiKey: string; |
| 20 | + |
| 21 | + constructor(apiKey: string) { |
| 22 | + super({ provider: 'figi', baseUrl: FIGI_BASE }); |
| 23 | + this.apiKey = apiKey; |
| 24 | + } |
| 25 | + |
| 26 | + protected buildRequest(req: ProviderRequest): { |
| 27 | + url: string; |
| 28 | + method: string; |
| 29 | + headers: Record<string, string>; |
| 30 | + body?: string; |
| 31 | + } { |
| 32 | + const params = req.params as Record<string, unknown>; |
| 33 | + const headers: Record<string, string> = { |
| 34 | + 'Content-Type': 'application/json', |
| 35 | + Accept: 'application/json', |
| 36 | + }; |
| 37 | + if (this.apiKey) { |
| 38 | + headers['X-OPENFIGI-APIKEY'] = this.apiKey; |
| 39 | + } |
| 40 | + |
| 41 | + switch (req.toolId) { |
| 42 | + case 'figi.map': { |
| 43 | + const mapping: Record<string, unknown> = { |
| 44 | + idType: String(params.id_type), |
| 45 | + idValue: String(params.id_value), |
| 46 | + }; |
| 47 | + if (params.exchange_code) mapping.exchCode = String(params.exchange_code); |
| 48 | + return { |
| 49 | + url: `${FIGI_BASE}/mapping`, |
| 50 | + method: 'POST', |
| 51 | + headers, |
| 52 | + body: JSON.stringify([mapping]), |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + case 'figi.search': { |
| 57 | + const search: Record<string, unknown> = { |
| 58 | + query: String(params.query), |
| 59 | + }; |
| 60 | + if (params.exchange_code) search.exchCode = String(params.exchange_code); |
| 61 | + if (params.security_type) search.securityType = String(params.security_type); |
| 62 | + return { |
| 63 | + url: `${FIGI_BASE}/search`, |
| 64 | + method: 'POST', |
| 65 | + headers, |
| 66 | + body: JSON.stringify(search), |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + case 'figi.filter': { |
| 71 | + const filter: Record<string, unknown> = {}; |
| 72 | + if (params.exchange_code) filter.exchCode = String(params.exchange_code); |
| 73 | + if (params.market_sector) filter.marketSector = String(params.market_sector); |
| 74 | + if (params.security_type) filter.securityType = String(params.security_type); |
| 75 | + return { |
| 76 | + url: `${FIGI_BASE}/filter`, |
| 77 | + method: 'POST', |
| 78 | + headers, |
| 79 | + body: JSON.stringify(filter), |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + default: |
| 84 | + throw { |
| 85 | + code: ProviderErrorCode.INVALID_RESPONSE, |
| 86 | + httpStatus: 502, |
| 87 | + message: `Unsupported tool: ${req.toolId}`, |
| 88 | + provider: this.provider, |
| 89 | + toolId: req.toolId, |
| 90 | + durationMs: 0, |
| 91 | + }; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected parseResponse(raw: ProviderRawResponse, req: ProviderRequest): unknown { |
| 96 | + const body = raw.body; |
| 97 | + |
| 98 | + switch (req.toolId) { |
| 99 | + case 'figi.map': |
| 100 | + return this.parseMap(body); |
| 101 | + case 'figi.search': |
| 102 | + return this.parseSearch(body as Record<string, unknown>); |
| 103 | + case 'figi.filter': |
| 104 | + return this.parseFilter(body as Record<string, unknown>); |
| 105 | + default: |
| 106 | + return body; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private parseMap(body: unknown): FigiMapOutput { |
| 111 | + // /mapping returns array of { data: [...] } or { error: "..." } |
| 112 | + const results = Array.isArray(body) ? body : []; |
| 113 | + const instruments: FigiInstrument[] = []; |
| 114 | + |
| 115 | + for (const r of results) { |
| 116 | + const rec = r as Record<string, unknown>; |
| 117 | + const data = (rec.data ?? []) as Record<string, unknown>[]; |
| 118 | + for (const item of data.slice(0, 10)) { |
| 119 | + instruments.push(this.toInstrument(item)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return { results: instruments, total: instruments.length }; |
| 124 | + } |
| 125 | + |
| 126 | + private parseSearch(body: Record<string, unknown>): FigiSearchOutput { |
| 127 | + const data = (body.data ?? []) as Record<string, unknown>[]; |
| 128 | + return { |
| 129 | + results: data.slice(0, 20).map((item) => this.toInstrument(item)), |
| 130 | + total: data.length, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + private parseFilter(body: Record<string, unknown>): FigiFilterOutput { |
| 135 | + const data = (body.data ?? []) as Record<string, unknown>[]; |
| 136 | + return { |
| 137 | + results: data.slice(0, 20).map((item) => this.toInstrument(item)), |
| 138 | + total: Number(body.total ?? data.length), |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + private toInstrument(item: Record<string, unknown>): FigiInstrument { |
| 143 | + return { |
| 144 | + figi: String(item.figi ?? ''), |
| 145 | + name: String(item.name ?? ''), |
| 146 | + ticker: String(item.ticker ?? ''), |
| 147 | + exchange_code: String(item.exchCode ?? ''), |
| 148 | + market_sector: String(item.marketSector ?? ''), |
| 149 | + security_type: String(item.securityType ?? item.securityType2 ?? ''), |
| 150 | + composite_figi: String(item.compositeFIGI ?? ''), |
| 151 | + share_class_figi: String(item.shareClassFIGI ?? ''), |
| 152 | + }; |
| 153 | + } |
| 154 | +} |
0 commit comments