Skip to content

Commit

Permalink
Merge pull request #11 from seamapi/handle-all-options
Browse files Browse the repository at this point in the history
Add tests for option parsing
  • Loading branch information
razor-x authored Oct 5, 2023
2 parents 7a9ec54 + a00807b commit 0669da0
Show file tree
Hide file tree
Showing 41 changed files with 626 additions and 463 deletions.
6 changes: 6 additions & 0 deletions ava.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { env } from 'node:process'

export default () => {
delete env.SEAM_API_KEY
delete env.SEAM_ENDPOINT
delete env.SEAM_API_URL

return {
ignoredByWatcher: ['tmp/**/*'],
files: ['**/*.test.ts', '!package/**/*'],
Expand Down
8 changes: 4 additions & 4 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ export type Handler<Options = EmptyOptions> = DefaultHandler<Options, Context>
type Context = DefaultContext & ClientContext

interface ClientContext {
client: SeamHttp
seam: SeamHttp
}

const commands = [workspace]

const createAppContext: MiddlewareFunction = async (argv) => {
const apiKey = argv['api-key']
if (typeof apiKey !== 'string') throw new Error('Missing Seam API key')
const client = SeamHttp.fromApiKey(apiKey)
argv['client'] = client
const seam = SeamHttp.fromApiKey(apiKey)
argv['seam'] = seam
}

const middleware = [...defaultMiddleware, createAppContext]

await landlubber<Context>(commands, { middleware })
.describe('api-key', 'Seam API key')
.string('api-key')
.default('api-key', env['SEAM_API_KEY'], 'SEAM_API_KEY')
.default('api-key', env.SEAM_API_KEY, 'SEAM_API_KEY')
.demandOption('api-key')
.parse()
4 changes: 2 additions & 2 deletions examples/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const describe: Describe = 'Get workspace'

export const builder: Builder = {}

export const handler: Handler<Options> = async ({ client, logger }) => {
const workspace = await client.workspaces.get()
export const handler: Handler<Options> = async ({ seam, logger }) => {
const workspace = await seam.workspaces.get()
logger.info({ workspace }, 'workspace')
}
14 changes: 7 additions & 7 deletions generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { readFile, writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'

import { openapi } from '@seamapi/types/connect'
import { camelCase, paramCase, pascalCase, snakeCase } from 'change-case'
import { camelCase, kebabCase, pascalCase, snakeCase } from 'change-case'
import { ESLint } from 'eslint'
import { format, resolveConfig } from 'prettier'

const rootClassPath = resolve('src', 'lib', 'seam', 'connect', 'client.ts')
const rootClassPath = resolve('src', 'lib', 'seam', 'connect', 'seam-http.ts')
const routeOutputPath = resolve('src', 'lib', 'seam', 'connect', 'routes')

const routePaths = [
Expand Down Expand Up @@ -237,7 +237,7 @@ import type { RouteRequestParams, RouteResponse, RouteRequestBody } from '@seama
import { Axios } from 'axios'
import type { SetNonNullable } from 'type-fest'
import { createAxiosClient } from 'lib/seam/connect/axios.js'
import { createClient } from 'lib/seam/connect/client.js'
import {
isSeamHttpOptionsWithApiKey,
isSeamHttpOptionsWithClient,
Expand All @@ -247,7 +247,7 @@ import {
type SeamHttpOptionsWithApiKey,
type SeamHttpOptionsWithClient,
type SeamHttpOptionsWithClientSessionToken,
} from 'lib/seam/connect/client-options.js'
} from 'lib/seam/connect/options.js'
import { parseOptions } from 'lib/seam/connect/parse-options.js'
${subresources
.map((subresource) => renderSubresourceImport(subresource, namespace))
Expand All @@ -259,7 +259,7 @@ const renderSubresourceImport = (
): string => `
import {
SeamHttp${pascalCase(namespace)}${pascalCase(subresource)}
} from './${paramCase(namespace)}-${paramCase(subresource)}.js'
} from './${kebabCase(namespace)}-${kebabCase(subresource)}.js'
`

const renderClass = (
Expand Down Expand Up @@ -389,13 +389,13 @@ const writeRoute = async (route: Route): Promise<void> => {
await write(
renderRoute(route, { constructors }),
routeOutputPath,
`${paramCase(route.namespace)}.ts`,
`${kebabCase(route.namespace)}.ts`,
)
}

const writeRoutesIndex = async (routes: Route[]): Promise<void> => {
const exports = routes.map(
(route) => `export * from './${paramCase(route.namespace)}.js'`,
(route) => `export * from './${kebabCase(route.namespace)}.js'`,
)
await write(exports.join('\n'), routeOutputPath, `index.ts`)
}
Expand Down
Loading

0 comments on commit 0669da0

Please sign in to comment.