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

Commit

Permalink
feat: add httpsnippet-lite convert method wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
tmastrom committed Aug 13, 2024
1 parent 0b19d5a commit 1d8615d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
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'
22 changes: 22 additions & 0 deletions packages/snippetz/src/snippetz.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,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"))`)
})
})
11 changes: 11 additions & 0 deletions packages/snippetz/src/snippetz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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 Target,
type HarRequest,
} from 'httpsnippet-lite'

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

Expand Down Expand Up @@ -51,5 +57,10 @@ export function snippetz() {
hasPlugin(target: string, client: string) {
return Boolean(this.findPlugin(target as TargetId, client as ClientId))
},

async convert(request: any, target: string, client?: string) {
const snippet = new HTTPSnippet(request as HarRequest)
return (await snippet.convert(target as Target, client)) 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.

0 comments on commit 1d8615d

Please sign in to comment.