Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: conditionally use snippetz or httpsnippet-lite in print method
Browse files Browse the repository at this point in the history
  • Loading branch information
tmastrom committed Aug 14, 2024
1 parent 1d8615d commit 82742c1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
25 changes: 23 additions & 2 deletions packages/snippetz/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@ export type { Request } from 'har-format'

export type Source = {
/** The language or environment. */
target: TargetId
target: ScalarTargetId
/** The identifier of the client. */
client: ClientId
/** The actual source code. */
code: string
}

export type TargetId = 'node' | 'js'
export type ScalarTargetId = 'node' | 'js'

export type ClientId = 'undici' | 'fetch' | 'ofetch'
import { type TargetId as SnippetTargetId } from 'httpsnippet-lite'

export type TargetId = ScalarTargetId | SnippetTargetId

export const ScalarTargetTypes = ['node', 'js'] as const

export const SnippetTargetTypes = [
'c',
'csharp',
'go',
'java',
'node',
'ocaml',
'php',
'python',
'ruby',
'shell',
'swift',
] as const

export const ScalarClientTypes = ['undici', 'fetch', 'ofetch'] as const
20 changes: 19 additions & 1 deletion packages/snippetz/src/snippetz.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { snippetz } from './snippetz'

describe('snippetz', async () => {
it('returns code for undici', async () => {
const snippet = snippetz().print('node', 'undici', {
const snippet = await snippetz().print('node', 'undici', {
url: 'https://example.com',
})

Expand All @@ -25,6 +25,24 @@ const { statusCode, body } = await request('https://example.com')`)
'ofetch',
])
})

it('returns code for python target', async () => {
const snippet = await snippetz().print('python', 'fetch', {
method: 'GET',
url: 'http://mockbin.com/request',
})

expect(snippet).toBe(`import http.client
conn = http.client.HTTPConnection("mockbin.com")
conn.request("GET", "/request")
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))`)
})
})

describe('plugins', async () => {
Expand Down
36 changes: 30 additions & 6 deletions packages/snippetz/src/snippetz.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { TargetId, ClientId, Request } from './core'
import type { TargetId, ClientId, Request, ScalarTargetId } from './core'
import {
ScalarTargetTypes,
SnippetTargetTypes,
ScalarClientTypes,
} from './core'
import { undici } from './plugins/node/undici'
import { fetch as nodeFetch } from './plugins/node/fetch'
import { fetch as jsFetch } from './plugins/js/fetch'
Expand All @@ -7,7 +12,7 @@ import { ofetch as nodeOFetch } from './plugins/node/ofetch'

import {
HTTPSnippet,
type TargetId as Target,
type TargetId as SnippetTargetId,
type HarRequest,
} from 'httpsnippet-lite'

Expand All @@ -22,8 +27,22 @@ export function snippetz() {
return plugin(request)
}
},
print(target: TargetId, client: ClientId, request: Partial<Request>) {
return this.get(target, client, request)?.code
async print(target: TargetId, client: string, request: Partial<Request>) {
// if target and client are valid scalar types
// use the plugin to convert the request
if (
ScalarTargetTypes.includes(target as ScalarTargetId) &&
ScalarClientTypes.includes(client as ClientId)
) {
return this.get(target, client as ClientId, request)?.code
}

// else use httpsnippet-lite to convert the request
if (SnippetTargetTypes.includes(target as any)) {
// TODO: add client parameter
return await this.convert(request, target)
}
// else return error
},
targets() {
return (
Expand Down Expand Up @@ -57,10 +76,15 @@ export function snippetz() {
hasPlugin(target: string, client: string) {
return Boolean(this.findPlugin(target as TargetId, client as ClientId))
},
// TODO: add client parameter

async convert(request: any, target: string, client?: string) {
async convert(request: any, target: string) {
const snippet = new HTTPSnippet(request as HarRequest)
return (await snippet.convert(target as Target, client)) as string

// https://www.npmjs.com/package/httpsnippet-lite#snippetconverttargetid-string-clientid-string-options-t
// snippet.convert(targetId: string, clientId?: string, options?: T)
// ERROR: convert method is looking for Client not ClientId
return (await snippet.convert(target as SnippetTargetId)) as string
},
}
}

0 comments on commit 82742c1

Please sign in to comment.