diff --git a/src/index.ts b/src/index.ts index a5fb510..906392c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,34 +1,29 @@ import { ByteBuffer } from 'flatbuffers'; import { WeatherApiResponse } from '@openmeteo/sdk/weather-api-response'; -async function test_api_fetch() { - const params = { - latitude: '52.52,48.1,48.4', - longitude: '13.41,9.31,8.5', - hourly: 'temperature_2m,precipitation,windspeed_10m', - timezone: 'auto', - current: 'temperature_2m,precipitation', - format: 'flatbuffers', - }; - const urlParams = new URLSearchParams(params).toString(); - const response = await fetch( - `https://api.open-meteo.com/v1/forecast?${urlParams}` - ); +async function weather_api( + url: string, + params: any +): Promise { + const urlParams = new URLSearchParams(params); + urlParams.set('format', 'flatbuffers'); + const response = await fetch(`${url}?${urlParams.toString()}`); const bb = await response.arrayBuffer(); const fb = new ByteBuffer(new Uint8Array(bb)); + // TODO: retry, error handling + const results: WeatherApiResponse[] = []; let pos = 0; while (pos < fb.capacity()) { + fb.setPosition(pos); const len = fb.readInt32(fb.position()); results.push( WeatherApiResponse.getSizePrefixedRootAsWeatherApiResponse(fb) ); pos += len + 4; - fb.setPosition(pos); } return results; } -export { test_api_fetch, WeatherApiResponse }; -// const myPackage = (taco = ''): string => `${taco} from my package`; +export { weather_api }; diff --git a/test/index.spec.ts b/test/index.spec.ts index 4b9e9d1..a10a3a7 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,83 +1,94 @@ -import { test_api_fetch } from '../src'; - -global.fetch = jest.fn(() => - Promise.resolve({ - // 2 location data, with hourly temp and precip - /* - "latitude": [52.54, 48.1], - "longitude": [13.41, 9.31], - "hourly": ["temperature_2m", "precipitation"], - "start_date": "2023-08-01", - "end_date": "2023-08-01", - }*/ - arrayBuffer: () => - Promise.resolve( - new Uint8Array([ - 228, 1, 0, 0, 28, 0, 0, 0, 24, 0, 30, 0, 24, 0, 20, 0, 16, 0, 15, 0, - 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 24, 0, 0, 0, 184, 0, 0, 0, 0, 65, - 135, 61, 0, 0, 0, 26, 0, 0, 112, 66, 112, 102, 86, 65, 0, 0, 82, 66, - 0, 0, 158, 0, 36, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, - 158, 0, 0, 0, 32, 0, 0, 0, 148, 0, 0, 0, 0, 75, 200, 100, 0, 0, 0, 0, - 128, 156, 201, 100, 0, 0, 0, 0, 16, 14, 0, 0, 0, 0, 0, 0, 144, 255, - 255, 255, 0, 0, 0, 9, 4, 0, 0, 0, 24, 0, 0, 0, 154, 153, 217, 63, 102, - 102, 6, 64, 0, 0, 128, 63, 205, 204, 76, 62, 205, 204, 204, 61, 0, 0, - 128, 63, 102, 102, 38, 64, 205, 204, 76, 64, 0, 0, 0, 64, 102, 102, - 102, 63, 154, 153, 153, 62, 205, 204, 44, 64, 154, 153, 153, 63, 205, - 204, 76, 62, 0, 0, 0, 0, 205, 204, 204, 61, 154, 153, 153, 62, 102, - 102, 102, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 8, 0, 12, 0, 8, 0, 7, 0, 8, 0, 0, 0, 0, 0, 0, 1, 4, 0, - 0, 0, 24, 0, 0, 0, 104, 145, 135, 65, 207, 247, 135, 65, 207, 247, - 135, 65, 156, 196, 134, 65, 104, 145, 133, 65, 207, 247, 133, 65, 207, - 247, 135, 65, 207, 247, 135, 65, 156, 196, 136, 65, 53, 94, 140, 65, - 207, 247, 145, 65, 53, 94, 144, 65, 156, 196, 144, 65, 104, 145, 151, - 65, 207, 247, 161, 65, 156, 196, 166, 65, 207, 247, 163, 65, 104, 145, - 145, 65, 53, 94, 144, 65, 2, 43, 137, 65, 53, 94, 134, 65, 53, 94, - 134, 65, 207, 247, 131, 65, 207, 247, 129, 65, 228, 1, 0, 0, 28, 0, 0, - 0, 24, 0, 30, 0, 24, 0, 20, 0, 16, 0, 15, 0, 8, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 4, 0, 24, 0, 0, 0, 184, 0, 0, 0, 0, 78, 237, 60, 0, 0, 0, 26, 0, - 0, 33, 68, 208, 204, 20, 65, 104, 102, 64, 66, 0, 0, 158, 0, 36, 0, - 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 158, 0, 0, 0, 32, 0, - 0, 0, 148, 0, 0, 0, 0, 75, 200, 100, 0, 0, 0, 0, 128, 156, 201, 100, - 0, 0, 0, 0, 16, 14, 0, 0, 0, 0, 0, 0, 144, 255, 255, 255, 0, 0, 0, 9, - 4, 0, 0, 0, 24, 0, 0, 0, 154, 153, 153, 62, 0, 0, 0, 63, 154, 153, - 153, 62, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0, - 128, 63, 154, 153, 153, 63, 205, 204, 12, 64, 154, 153, 217, 63, 51, - 51, 179, 63, 154, 153, 153, 62, 205, 204, 76, 63, 0, 0, 0, 64, 205, - 204, 76, 64, 205, 204, 204, 62, 205, 204, 204, 61, 205, 204, 204, 61, - 0, 0, 0, 0, 205, 204, 204, 61, 205, 204, 204, 61, 205, 204, 76, 62, - 205, 204, 204, 61, 8, 0, 12, 0, 8, 0, 7, 0, 8, 0, 0, 0, 0, 0, 0, 1, 4, - 0, 0, 0, 24, 0, 0, 0, 180, 72, 134, 65, 26, 175, 132, 65, 231, 123, - 133, 65, 26, 175, 136, 65, 26, 175, 136, 65, 129, 21, 137, 65, 129, - 21, 137, 65, 129, 21, 131, 65, 180, 72, 132, 65, 78, 226, 131, 65, 78, - 226, 133, 65, 129, 21, 135, 65, 231, 123, 143, 65, 231, 123, 151, 65, - 26, 175, 142, 65, 129, 21, 145, 65, 180, 72, 142, 65, 129, 21, 139, - 65, 78, 226, 133, 65, 26, 175, 128, 65, 206, 247, 118, 65, 2, 43, 110, - 65, 155, 196, 103, 65, 206, 247, 94, 65, - ]).buffer - ), - json: () => Promise.resolve({ test: 100 }), - }) -) as jest.Mock; +import { weather_api } from '../src'; describe('index', () => { describe('myPackage', () => { test('aaaaa', async () => { - const a = await test_api_fetch(); - console.log(a[0].latitude()); - console.log(a.length); - //expect(a.latitude()).toBe(47); + // 2 location data, with hourly temp and precip + global.fetch = jest.fn(() => + Promise.resolve({ + arrayBuffer: () => + Promise.resolve( + new Uint8Array([ + 228, 1, 0, 0, 28, 0, 0, 0, 24, 0, 30, 0, 24, 0, 20, 0, 16, 0, + 15, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 24, 0, 0, 0, 184, 0, + 0, 0, 0, 65, 135, 61, 0, 0, 0, 26, 0, 0, 112, 66, 112, 102, 86, + 65, 0, 0, 82, 66, 0, 0, 158, 0, 36, 0, 12, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 158, + 0, 0, 0, 32, 0, 0, 0, 148, 0, 0, 0, 0, 75, 200, 100, 0, 0, 0, 0, + 128, 156, 201, 100, 0, 0, 0, 0, 16, 14, 0, 0, 0, 0, 0, 0, 144, + 255, 255, 255, 0, 0, 0, 9, 4, 0, 0, 0, 24, 0, 0, 0, 154, 153, + 217, 63, 102, 102, 6, 64, 0, 0, 128, 63, 205, 204, 76, 62, 205, + 204, 204, 61, 0, 0, 128, 63, 102, 102, 38, 64, 205, 204, 76, 64, + 0, 0, 0, 64, 102, 102, 102, 63, 154, 153, 153, 62, 205, 204, 44, + 64, 154, 153, 153, 63, 205, 204, 76, 62, 0, 0, 0, 0, 205, 204, + 204, 61, 154, 153, 153, 62, 102, 102, 102, 63, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 12, + 0, 8, 0, 7, 0, 8, 0, 0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 24, 0, 0, 0, + 104, 145, 135, 65, 207, 247, 135, 65, 207, 247, 135, 65, 156, + 196, 134, 65, 104, 145, 133, 65, 207, 247, 133, 65, 207, 247, + 135, 65, 207, 247, 135, 65, 156, 196, 136, 65, 53, 94, 140, 65, + 207, 247, 145, 65, 53, 94, 144, 65, 156, 196, 144, 65, 104, 145, + 151, 65, 207, 247, 161, 65, 156, 196, 166, 65, 207, 247, 163, + 65, 104, 145, 145, 65, 53, 94, 144, 65, 2, 43, 137, 65, 53, 94, + 134, 65, 53, 94, 134, 65, 207, 247, 131, 65, 207, 247, 129, 65, + 228, 1, 0, 0, 28, 0, 0, 0, 24, 0, 30, 0, 24, 0, 20, 0, 16, 0, + 15, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 24, 0, 0, 0, 184, 0, + 0, 0, 0, 78, 237, 60, 0, 0, 0, 26, 0, 0, 33, 68, 208, 204, 20, + 65, 104, 102, 64, 66, 0, 0, 158, 0, 36, 0, 12, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, + 158, 0, 0, 0, 32, 0, 0, 0, 148, 0, 0, 0, 0, 75, 200, 100, 0, 0, + 0, 0, 128, 156, 201, 100, 0, 0, 0, 0, 16, 14, 0, 0, 0, 0, 0, 0, + 144, 255, 255, 255, 0, 0, 0, 9, 4, 0, 0, 0, 24, 0, 0, 0, 154, + 153, 153, 62, 0, 0, 0, 63, 154, 153, 153, 62, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0, 128, 63, 154, 153, + 153, 63, 205, 204, 12, 64, 154, 153, 217, 63, 51, 51, 179, 63, + 154, 153, 153, 62, 205, 204, 76, 63, 0, 0, 0, 64, 205, 204, 76, + 64, 205, 204, 204, 62, 205, 204, 204, 61, 205, 204, 204, 61, 0, + 0, 0, 0, 205, 204, 204, 61, 205, 204, 204, 61, 205, 204, 76, 62, + 205, 204, 204, 61, 8, 0, 12, 0, 8, 0, 7, 0, 8, 0, 0, 0, 0, 0, 0, + 1, 4, 0, 0, 0, 24, 0, 0, 0, 180, 72, 134, 65, 26, 175, 132, 65, + 231, 123, 133, 65, 26, 175, 136, 65, 26, 175, 136, 65, 129, 21, + 137, 65, 129, 21, 137, 65, 129, 21, 131, 65, 180, 72, 132, 65, + 78, 226, 131, 65, 78, 226, 133, 65, 129, 21, 135, 65, 231, 123, + 143, 65, 231, 123, 151, 65, 26, 175, 142, 65, 129, 21, 145, 65, + 180, 72, 142, 65, 129, 21, 139, 65, 78, 226, 133, 65, 26, 175, + 128, 65, 206, 247, 118, 65, 2, 43, 110, 65, 155, 196, 103, 65, + 206, 247, 94, 65, + ]).buffer + ), + json: () => Promise.resolve({ test: 100 }), + }) + ) as jest.Mock; + + const params = { + latitude: [52.54, 48.1], + longitude: [13.41, 9.31], + hourly: ['temperature_2m', 'precipitation'], + start_date: '2023-08-01', + end_date: '2023-08-01', + format: 'flatbuffers', + }; + const url = 'https://api.open-meteo.com/v1/forecast'; + const a = await weather_api(url, params); + expect(a).toHaveLength(2); + expect(a[0].latitude()).toBeCloseTo(52.5, 2); + expect(a[1].latitude()).toBeCloseTo(48.1, 2); + const temperature_2m = a[0].hourly()?.temperature2m()?.valuesArray(); + expect(temperature_2m).toBeDefined(); + expect(temperature_2m).toHaveLength(24); + expect(temperature_2m![0]).toBeCloseTo(16.9459991, 2); }); }); });