From 82742c182c318700f7b23ae7bc46d94bd7211069 Mon Sep 17 00:00:00 2001 From: tmastrom Date: Tue, 13 Aug 2024 17:40:45 -0700 Subject: [PATCH] feat: conditionally use snippetz or httpsnippet-lite in print method --- packages/snippetz/src/core/types.ts | 25 ++++++++++++++++-- packages/snippetz/src/snippetz.test.ts | 20 +++++++++++++- packages/snippetz/src/snippetz.ts | 36 +++++++++++++++++++++----- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/packages/snippetz/src/core/types.ts b/packages/snippetz/src/core/types.ts index b105450..05a1066 100644 --- a/packages/snippetz/src/core/types.ts +++ b/packages/snippetz/src/core/types.ts @@ -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 diff --git a/packages/snippetz/src/snippetz.test.ts b/packages/snippetz/src/snippetz.test.ts index aeb8d7f..ba13bec 100644 --- a/packages/snippetz/src/snippetz.test.ts +++ b/packages/snippetz/src/snippetz.test.ts @@ -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', }) @@ -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 () => { diff --git a/packages/snippetz/src/snippetz.ts b/packages/snippetz/src/snippetz.ts index 6e67296..af951ce 100644 --- a/packages/snippetz/src/snippetz.ts +++ b/packages/snippetz/src/snippetz.ts @@ -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' @@ -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' @@ -22,8 +27,22 @@ export function snippetz() { return plugin(request) } }, - print(target: TargetId, client: ClientId, request: Partial) { - return this.get(target, client, request)?.code + async print(target: TargetId, client: string, request: Partial) { + // 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 ( @@ -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 }, } }