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

Commit 82742c1

Browse files
committed
feat: conditionally use snippetz or httpsnippet-lite in print method
1 parent 1d8615d commit 82742c1

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

packages/snippetz/src/core/types.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,34 @@ export type { Request } from 'har-format'
22

33
export type Source = {
44
/** The language or environment. */
5-
target: TargetId
5+
target: ScalarTargetId
66
/** The identifier of the client. */
77
client: ClientId
88
/** The actual source code. */
99
code: string
1010
}
1111

12-
export type TargetId = 'node' | 'js'
12+
export type ScalarTargetId = 'node' | 'js'
1313

1414
export type ClientId = 'undici' | 'fetch' | 'ofetch'
15+
import { type TargetId as SnippetTargetId } from 'httpsnippet-lite'
16+
17+
export type TargetId = ScalarTargetId | SnippetTargetId
18+
19+
export const ScalarTargetTypes = ['node', 'js'] as const
20+
21+
export const SnippetTargetTypes = [
22+
'c',
23+
'csharp',
24+
'go',
25+
'java',
26+
'node',
27+
'ocaml',
28+
'php',
29+
'python',
30+
'ruby',
31+
'shell',
32+
'swift',
33+
] as const
34+
35+
export const ScalarClientTypes = ['undici', 'fetch', 'ofetch'] as const

packages/snippetz/src/snippetz.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { snippetz } from './snippetz'
33

44
describe('snippetz', async () => {
55
it('returns code for undici', async () => {
6-
const snippet = snippetz().print('node', 'undici', {
6+
const snippet = await snippetz().print('node', 'undici', {
77
url: 'https://example.com',
88
})
99

@@ -25,6 +25,24 @@ const { statusCode, body } = await request('https://example.com')`)
2525
'ofetch',
2626
])
2727
})
28+
29+
it('returns code for python target', async () => {
30+
const snippet = await snippetz().print('python', 'fetch', {
31+
method: 'GET',
32+
url: 'http://mockbin.com/request',
33+
})
34+
35+
expect(snippet).toBe(`import http.client
36+
37+
conn = http.client.HTTPConnection("mockbin.com")
38+
39+
conn.request("GET", "/request")
40+
41+
res = conn.getresponse()
42+
data = res.read()
43+
44+
print(data.decode("utf-8"))`)
45+
})
2846
})
2947

3048
describe('plugins', async () => {

packages/snippetz/src/snippetz.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { TargetId, ClientId, Request } from './core'
1+
import type { TargetId, ClientId, Request, ScalarTargetId } from './core'
2+
import {
3+
ScalarTargetTypes,
4+
SnippetTargetTypes,
5+
ScalarClientTypes,
6+
} from './core'
27
import { undici } from './plugins/node/undici'
38
import { fetch as nodeFetch } from './plugins/node/fetch'
49
import { fetch as jsFetch } from './plugins/js/fetch'
@@ -7,7 +12,7 @@ import { ofetch as nodeOFetch } from './plugins/node/ofetch'
712

813
import {
914
HTTPSnippet,
10-
type TargetId as Target,
15+
type TargetId as SnippetTargetId,
1116
type HarRequest,
1217
} from 'httpsnippet-lite'
1318

@@ -22,8 +27,22 @@ export function snippetz() {
2227
return plugin(request)
2328
}
2429
},
25-
print(target: TargetId, client: ClientId, request: Partial<Request>) {
26-
return this.get(target, client, request)?.code
30+
async print(target: TargetId, client: string, request: Partial<Request>) {
31+
// if target and client are valid scalar types
32+
// use the plugin to convert the request
33+
if (
34+
ScalarTargetTypes.includes(target as ScalarTargetId) &&
35+
ScalarClientTypes.includes(client as ClientId)
36+
) {
37+
return this.get(target, client as ClientId, request)?.code
38+
}
39+
40+
// else use httpsnippet-lite to convert the request
41+
if (SnippetTargetTypes.includes(target as any)) {
42+
// TODO: add client parameter
43+
return await this.convert(request, target)
44+
}
45+
// else return error
2746
},
2847
targets() {
2948
return (
@@ -57,10 +76,15 @@ export function snippetz() {
5776
hasPlugin(target: string, client: string) {
5877
return Boolean(this.findPlugin(target as TargetId, client as ClientId))
5978
},
79+
// TODO: add client parameter
6080

61-
async convert(request: any, target: string, client?: string) {
81+
async convert(request: any, target: string) {
6282
const snippet = new HTTPSnippet(request as HarRequest)
63-
return (await snippet.convert(target as Target, client)) as string
83+
84+
// https://www.npmjs.com/package/httpsnippet-lite#snippetconverttargetid-string-clientid-string-options-t
85+
// snippet.convert(targetId: string, clientId?: string, options?: T)
86+
// ERROR: convert method is looking for Client not ClientId
87+
return (await snippet.convert(target as SnippetTargetId)) as string
6488
},
6589
}
6690
}

0 commit comments

Comments
 (0)