diff --git a/__tests__/platform.test.ts b/__tests__/platform.test.ts new file mode 100644 index 0000000..2a22453 --- /dev/null +++ b/__tests__/platform.test.ts @@ -0,0 +1,37 @@ +import nock from 'nock' +import { describe, it, expect } from '@jest/globals' +import { FLY_API_GRAPHQL } from '../src/client' +import { createClient } from '../src/main' + +const fly = createClient(process.env.FLY_API_TOKEN || 'test-token') + +describe('Platform', () => { + it('get regions', async () => { + const mockResponse = { + data: { + platform: { + requestRegion: 'sin', + regions: [ + { + name: 'Amsterdam, Netherlands', + code: 'ams', + latitude: 52.374342, + longitude: 4.895439, + gatewayAvailable: true, + requiresPaidPlan: false, + }, + ], + }, + }, + } + + nock(FLY_API_GRAPHQL).post('/graphql').reply(200, mockResponse) + + const data = await fly.Platform.getRegions() + console.dir(data, { depth: 5 }) + + expect(data).toBeDefined() + expect(data.platform).toBeDefined() + expect(data.platform.regions).toBeInstanceOf(Array) + }) +}) diff --git a/__tests__/regions.test.ts b/__tests__/regions.test.ts index 8f05d12..88794f5 100644 --- a/__tests__/regions.test.ts +++ b/__tests__/regions.test.ts @@ -6,35 +6,22 @@ import { createClient } from '../src/main' const fly = createClient(process.env.FLY_API_TOKEN || 'test-token') describe('regions', () => { - it('get regions', async () => { - const mockResponse = { - data: { - platform: { - requestRegion: 'sin', - regions: [ - { - name: 'Amsterdam, Netherlands', - code: 'ams', - latitude: 52.374342, - longitude: 4.895439, - gatewayAvailable: true, - requiresPaidPlan: false, - }, - // Add more mock regions as needed - ], + it('get nearest regions', async () => { + nock(FLY_API_GRAPHQL) + .post('/graphql') + .reply(200, { + data: { + nearestRegion: { + name: 'Amsterdam, Netherlands', + code: 'ams', + latitude: 52.374342, + longitude: 4.895439, + gatewayAvailable: true, + requiresPaidPlan: false, + }, }, - }, - } - - nock(FLY_API_GRAPHQL).post('/graphql').reply(200, mockResponse) - - const data = await fly.Regions.getRegions() + }) + const data = await fly.Regions.getNearestRegion() console.dir(data, { depth: 5 }) - - // Optionally, add assertions to verify the response - expect(data).toBeDefined() - expect(data.platform).toBeDefined() - expect(data.platform.regions).toBeInstanceOf(Array) - // Add more assertions as needed }) }) diff --git a/src/client.ts b/src/client.ts index d605501..f1d92dc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6,6 +6,7 @@ import { Organization } from './lib/organization' import { Secret } from './lib/secret' import { Volume } from './lib/volume' import { Regions } from './lib/regions' +import { Platform } from './lib/platform' export const FLY_API_GRAPHQL = 'https://api.fly.io' export const FLY_API_HOSTNAME = 'https://api.machines.dev' @@ -37,9 +38,10 @@ class Client { private apiKey: string App: App Machine: Machine - Regions: Regions Network: Network Organization: Organization + Platform: Platform + Regions: Regions Secret: Secret Volume: Volume @@ -53,8 +55,9 @@ class Client { this.App = new App(this) this.Machine = new Machine(this) this.Network = new Network(this) - this.Regions = new Regions(this) this.Organization = new Organization(this) + this.Platform = new Platform(this) + this.Regions = new Regions(this) this.Secret = new Secret(this) this.Volume = new Volume(this) } diff --git a/src/lib/platform.ts b/src/lib/platform.ts new file mode 100644 index 0000000..a73d4fc --- /dev/null +++ b/src/lib/platform.ts @@ -0,0 +1,41 @@ +import Client from '../client' +import { RegionResponse } from './regions' + +interface PlatformResponse { + requestRegion: string + regions: RegionResponse[] +} + +export interface GetRegionsOutput { + platform: PlatformResponse +} + +// Ref: https://github.com/superfly/flyctl/blob/master/api/resource_platform.go +const getRegionsQuery = `query { + platform { + requestRegion + regions { + name + code + latitude + longitude + gatewayAvailable + requiresPaidPlan + } + } +}` + +export class Platform { + private client: Client + + constructor(client: Client) { + this.client = client + } + + async getRegions(): Promise { + return this.client.gqlPostOrThrow({ + query: getRegionsQuery, + variables: {}, + }) + } +} diff --git a/src/lib/regions.ts b/src/lib/regions.ts index b8caaf3..8a6eaf8 100644 --- a/src/lib/regions.ts +++ b/src/lib/regions.ts @@ -1,6 +1,6 @@ import Client from '../client' -interface RegionResponse { +export interface RegionResponse { name: string code: string latitude: number @@ -9,29 +9,23 @@ interface RegionResponse { requiresPaidPlan: boolean } -interface PlatformResponse { - requestRegion: string - regions: RegionResponse[] +export interface GetNearestRegionsOutput { + nearestRegion: RegionResponse } -export interface GetRegionsOutput { - platform: PlatformResponse -} - -// Ref: https://github.com/superfly/flyctl/blob/master/api/resource_platform.go -const getRegionsQuery = `query { - platform { - requestRegion - regions { - name - code - latitude - longitude - gatewayAvailable - requiresPaidPlan - } +// Ref: https://github.com/superfly/flyctl/blob/master/api/resource_regions.go +const getNearestRegionsQuery = ` +query { + nearestRegion { + name + code + latitude + longitude + gatewayAvailable + requiresPaidPlan } -}` +} +` export class Regions { private client: Client @@ -40,9 +34,9 @@ export class Regions { this.client = client } - async getRegions(): Promise { + async getNearestRegion(): Promise { return this.client.gqlPostOrThrow({ - query: getRegionsQuery, + query: getNearestRegionsQuery, variables: {}, }) }