Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions __tests__/platform.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
43 changes: 15 additions & 28 deletions __tests__/regions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
7 changes: 5 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand All @@ -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)
}
Expand Down
41 changes: 41 additions & 0 deletions src/lib/platform.ts
Original file line number Diff line number Diff line change
@@ -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<GetRegionsOutput> {
return this.client.gqlPostOrThrow({
query: getRegionsQuery,
variables: {},
})
}
}
40 changes: 17 additions & 23 deletions src/lib/regions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Client from '../client'

interface RegionResponse {
export interface RegionResponse {
name: string
code: string
latitude: number
Expand All @@ -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
Expand All @@ -40,9 +34,9 @@ export class Regions {
this.client = client
}

async getRegions(): Promise<GetRegionsOutput> {
async getNearestRegion(): Promise<GetNearestRegionsOutput> {
return this.client.gqlPostOrThrow({
query: getRegionsQuery,
query: getNearestRegionsQuery,
variables: {},
})
}
Expand Down