|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { HttpService as NestHttpService } from '@nestjs/axios'; |
| 3 | + |
| 4 | +@Injectable() |
| 5 | +export class GraphqlService { |
| 6 | + constructor(private readonly httpService: NestHttpService) {} |
| 7 | + |
| 8 | + async makeGraphqlRequest({ |
| 9 | + url, |
| 10 | + method, |
| 11 | + headers, |
| 12 | + body, |
| 13 | + contentType, |
| 14 | + }: { |
| 15 | + url: string; |
| 16 | + method: string; |
| 17 | + headers: string; |
| 18 | + body: any; |
| 19 | + contentType: string; |
| 20 | + }): Promise<{ status: string; data: any; headers: any }> { |
| 21 | + try { |
| 22 | + // Parse headers from stringified JSON |
| 23 | + const parsedHeaders: Record<string, string> = {}; |
| 24 | + let headersArray; |
| 25 | + |
| 26 | + try { |
| 27 | + headersArray = JSON.parse(headers); |
| 28 | + if (Array.isArray(headersArray)) { |
| 29 | + headersArray.forEach((item: any) => { |
| 30 | + parsedHeaders[item.key] = item.value; |
| 31 | + }); |
| 32 | + } |
| 33 | + } catch (headerError) { |
| 34 | + console.error('Error parsing headers:', headerError); |
| 35 | + throw new Error('Invalid headers format'); |
| 36 | + } |
| 37 | + |
| 38 | + // Prepare the request configuration |
| 39 | + const config: any = { |
| 40 | + url, |
| 41 | + method, |
| 42 | + headers: parsedHeaders, |
| 43 | + data: null, |
| 44 | + }; |
| 45 | + |
| 46 | + // Handle body based on content type |
| 47 | + try { |
| 48 | + switch (contentType) { |
| 49 | + case 'application/json': |
| 50 | + config.data = typeof body === 'string' ? JSON.parse(body) : body; |
| 51 | + break; |
| 52 | + |
| 53 | + default: |
| 54 | + break; |
| 55 | + } |
| 56 | + } catch (bodyError) { |
| 57 | + console.error('Error processing request body:', bodyError); |
| 58 | + throw new Error('Invalid request body format'); |
| 59 | + } |
| 60 | + |
| 61 | + // Add custom user agent |
| 62 | + config.headers['User-Agent'] = 'SparrowRuntime/1.0.0'; |
| 63 | + |
| 64 | + try { |
| 65 | + const response = await this.httpService.axiosRef({ |
| 66 | + url: config.url, |
| 67 | + method: config.method, |
| 68 | + headers: config.headers, |
| 69 | + data: config.data, |
| 70 | + }); |
| 71 | + const resp = { |
| 72 | + status: |
| 73 | + response.status + ' ' + (response.statusText || 'Unknown Status'), |
| 74 | + data: response.data, |
| 75 | + headers: response.headers, |
| 76 | + }; |
| 77 | + return resp; |
| 78 | + } catch (axiosError: any) { |
| 79 | + return { |
| 80 | + status: axiosError.response?.status |
| 81 | + ? axiosError.response?.status + |
| 82 | + ' ' + |
| 83 | + (axiosError.response?.statusText || 'Unknown Status') |
| 84 | + : null, |
| 85 | + data: axiosError.response?.data || { message: axiosError.message }, |
| 86 | + headers: axiosError.response?.headers, |
| 87 | + }; |
| 88 | + } |
| 89 | + } catch (error: any) { |
| 90 | + console.error('HTTP Service Error:', error); |
| 91 | + throw new Error(error.message || 'Unknown error occurred'); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments