|
| 1 | +import config from "../config"; |
| 2 | +import * as Wreck from "@hapi/wreck"; |
| 3 | + |
| 4 | +interface FormJson { |
| 5 | + startPage: string; |
| 6 | + pages: any[]; |
| 7 | + sections: any[]; |
| 8 | + name: string; |
| 9 | + version?: number; |
| 10 | + conditions?: any[]; |
| 11 | + lists?: any[]; |
| 12 | + metadata?: any; |
| 13 | + fees?: any[]; |
| 14 | + outputs?: any[]; |
| 15 | + skipSummary?: boolean; |
| 16 | + [key: string]: any; |
| 17 | +} |
| 18 | + |
| 19 | +export interface FormData { |
| 20 | + name: string; |
| 21 | + form_json: FormJson; |
| 22 | +} |
| 23 | + |
| 24 | +export interface FormResponse { |
| 25 | + id: string; |
| 26 | + name: string; |
| 27 | + created_at: string; |
| 28 | + updated_at: string; |
| 29 | + published_at: string | null; |
| 30 | + draft_json: FormJson; |
| 31 | + published_json: FormJson; |
| 32 | + is_published: boolean; |
| 33 | +} |
| 34 | + |
| 35 | +export class PreAwardApiClient { |
| 36 | + private baseUrl: string; |
| 37 | + |
| 38 | + constructor() { |
| 39 | + this.baseUrl = config.preAwardApiUrl; |
| 40 | + } |
| 41 | + |
| 42 | + async createOrUpdateForm(formData: FormData): Promise<FormResponse>{ |
| 43 | + const payload = formData; |
| 44 | + |
| 45 | + try{ |
| 46 | + const { payload: responseData } = await Wreck.post( |
| 47 | + `${this.baseUrl}/forms`, |
| 48 | + { |
| 49 | + payload: JSON.stringify(payload), |
| 50 | + headers: { |
| 51 | + 'Content-Type': 'application/json' |
| 52 | + } |
| 53 | + } |
| 54 | + ); |
| 55 | + const parsedData = JSON.parse((responseData as Buffer).toString()); |
| 56 | + return parsedData as FormResponse; |
| 57 | + } |
| 58 | + catch (error) { |
| 59 | + throw error; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + async getAllForms(): Promise<FormResponse[]> { |
| 64 | + try { |
| 65 | + const { payload: responseData } = await Wreck.get( |
| 66 | + `${this.baseUrl}/forms`, |
| 67 | + { |
| 68 | + headers: { |
| 69 | + 'Content-Type': 'application/json' |
| 70 | + } |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + const parsedData = JSON.parse((responseData as Buffer).toString()); |
| 75 | + return parsedData as FormResponse[]; |
| 76 | + } catch (error) { |
| 77 | + throw error; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async getFormDraft(name: string): Promise<FormJson>{ |
| 82 | + try{ |
| 83 | + const { payload: responseData } = await Wreck.get( |
| 84 | + `${this.baseUrl}/forms/${name}/draft`, |
| 85 | + { |
| 86 | + headers: { |
| 87 | + 'Content-Type': 'application/json' |
| 88 | + } |
| 89 | + } |
| 90 | + ); |
| 91 | + const parsedData = JSON.parse((responseData as Buffer).toString()); |
| 92 | + return parsedData as FormJson; |
| 93 | + } |
| 94 | + catch (error) { |
| 95 | + throw error; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export const preAwardApiClient = new PreAwardApiClient(); |
0 commit comments