Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/planetscale

src/list-organizations.ts

45 lines · 1.5 KB · TypeScript
import { planetscaleRequest } from './client.ts'
import { mapOrganization } from './models.ts'
import type { PlanetscaleAuthInput, PlanetscaleOrganizationSummary, PlanetscalePageInfo } from './types.ts'
import { clampInt, compactRecord, optionalString, requireRecord } from './types.ts'

export type ListOrganizationsResult = {
	items: Array<PlanetscaleOrganizationSummary>
	pageInfo: PlanetscalePageInfo
}

/**
 * List organizations the service token can access.
 * @example
 * import listOrganizations from 'kody:@kody/planetscale/list-organizations'
 * const { items } = await listOrganizations()
 */
export async function listOrganizations(
	input: PlanetscaleAuthInput & { page?: number; perPage?: number } = {},
): Promise<ListOrganizationsResult> {
	const result = await planetscaleRequest<Array<unknown>>({
		...input,
		operation: 'organizations.read',
		path: '/organizations',
		query: compactRecord({
			page: clampInt(input.page, 1, 100, 1, 'page'),
			per_page: clampInt(input.perPage, 1, 100, 25),
		}),
	})
	return {
		items: (Array.isArray(result.data) ? result.data : []).map(mapOrganization),
		pageInfo: result.pageInfo,
	}
}

export default async function listOrganizationsEntrypoint(
	params: PlanetscaleAuthInput & Record<string, unknown> = {},
) {
	const input = requireRecord(params, 'list-organizations')
	return listOrganizations({
		page: input.page as number | undefined,
		perPage: input.perPage as number | undefined,
		account: optionalString(input.account, 'account'),
		secretName: optionalString(input.secretName, 'secretName'),
	})
}