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

feat: unify snippetz and httpsnippet-lite #47

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/snippetz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"devDependencies": {
"@scalar/build-tooling": "^0.1.10",
"@types/har-format": "^1.2.15"
"@types/har-format": "^1.2.15",
"httpsnippet-lite": "^3.0.5"
}
}
2 changes: 2 additions & 0 deletions packages/snippetz/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './utils/isKeyNeedsQuotes'
export * from './utils/objectToString'

export * from './types'

export { availableTargets as allTargets, HTTPSnippet } from 'httpsnippet-lite'
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
42 changes: 41 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 Expand Up @@ -69,3 +87,25 @@ describe('hasPlugin', async () => {
expect(result).toBe(false)
})
})

describe('convert', async () => {
it('converts a request outside of the scalar types to snippet using httpsnippet-lite', async () => {
const request = {
method: 'GET',
url: 'http://mockbin.com/request',
}

const snippet = await snippetz().convert(request, 'python')

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"))`)
})
})
41 changes: 38 additions & 3 deletions packages/snippetz/src/snippetz.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
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'
import { ofetch as jsOFetch } from './plugins/js/ofetch'
import { ofetch as nodeOFetch } from './plugins/node/ofetch'

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

export function snippetz() {
const plugins = [undici, nodeFetch, jsFetch, jsOFetch, nodeOFetch]

Expand All @@ -16,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 @@ -51,5 +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) {
const snippet = new HTTPSnippet(request as HarRequest)

// 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
},
}
}
61 changes: 61 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading