Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernization #359

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = {
'plugin:jest/all',
'plugin:jest-formatting/strict',
'plugin:eslint-comments/recommended',
'prettier',
],
plugins: ['eslint-comments', 'simple-import-sort', 'jest', 'jest-formatting'],
rules: {
Expand All @@ -26,5 +25,6 @@ module.exports = {
'simple-import-sort/exports': 'error',
'jest/prefer-expect-assertions': 'off',
'jest/require-hook': 'off',
'jest/max-expects': 'warn',
},
};
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
The MIT License (MIT)

Copyright (c) 2023 Jakub Rożek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


The MIT License (MIT)

Copyright (c) 2022 Kong (https://www.konghq.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
196 changes: 30 additions & 166 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
# HTTPSnippet
# HTTPSnippet-lite

[![version][npm-version]][npm-url] [![License][npm-license]][license-url]

> HTTP Request snippet generator for _many_ languages & tools including: `cURL`, `HTTPie`, `JavaScript`, `Node`, `C`, `Java`, `PHP`, `Objective-C`, `Swift`, `Python`, `Ruby`, `C#`, `Go`, `OCaml` and [more](https://github.com/Kong/httpsnippet/wiki/Targets)!

Relies on the popular [HAR](http://www.softwareishard.com/blog/har-12-spec/#request) format to import data and describe HTTP calls.

See it in action on companion service: [APIembed](https://apiembed.com)
[![Build](https://github.com/httpsnippet-lite/workflows/CI/badge.svg)](https://github.com/httpsnippet-lite)

[![Build](https://github.com/Kong/httpsnippet/actions/workflows/build.yml/badge.svg)](https://github.com/Kong/httpsnippet/actions/workflows/build.yml) [![Downloads][npm-downloads]][npm-url]

- [HTTPSnippet](#httpsnippet)
- [HTTPSnippet](#httpsnippet-lite)
- [Quickstart](#quickstart)
- [Core Concepts](#core-concepts)
- [CLI Quickstart](#cli-quickstart)
- [TypeScript Library Quickstart](#typescript-library-quickstart)
- [CLI Usage](#cli-usage)
- [CLI Installation](#cli-installation)
- [Example](#example)
- [TypeScript Library Usage](#typescript-library-usage)
- [Library Installation](#library-installation)
- [Types](#types)
Expand All @@ -37,8 +32,6 @@ See it in action on companion service: [APIembed](https://apiembed.com)
- [`addTarget`](#addtarget)
- [`isClient`](#isclient)
- [`addTargetClient`](#addtargetclient)
- [Bugs and feature requests](#bugs-and-feature-requests)
- [Contributing](#contributing)

## Quickstart

Expand All @@ -54,153 +47,31 @@ See it in action on companion service: [APIembed](https://apiembed.com)
### CLI Quickstart

```shell
httpsnippet har.json \ # the path your input file (must be in HAR format)
--target shell \ # your desired language
--client curl \ # your desired language library
--output ./examples \ # an output directory, otherwise will just output to Stdout
--options '{ "indent": false }' # any client options as a JSON string
npm install --save httpsnippet-lite
```

### TypeScript Library Quickstart

```ts
import { HTTPSnippet } from 'httpsnippet';
import { HTTPSnippet } from 'httpsnippet-lite';

const snippet = new HTTPSnippet({
method: 'GET',
url: 'http://mockbin.com/request',
});

const options = { indent: '\t' };
const output = snippet.convert('shell', 'curl', options);
const output = await snippet.convert('shell', 'curl', options);
console.log(output);
```

## CLI Usage

### CLI Installation

| NPM | Yarn |
| ------------------------------------------- | -------------------------------------- |
| <pre>npm install --global httpsnippet</pre> | <pre>yarn global add httpsnippet</pre> |

```text
httpsnippet [harFilePath]

the default command

Options:
--help Show help [boolean]
--version Show version number [boolean]
-t, --target target output [string] [required]
-c, --client language client [string]
-o, --output write output to directory [string]
-x, --options provide extra options for the target/client [string]

Examples:
httpsnippet my_har.json --target rust --client actix --output my_src_directory
```

### Example

The input to HTTPSnippet is any valid [HAR Request Object](http://www.softwareishard.com/blog/har-12-spec/#request), or full [HAR](http://www.softwareishard.com/blog/har-12-spec/#log) log format.

<details>
<summary>`example.json`</summary>

```json
{
"method": "POST",
"url": "http://mockbin.com/har?key=value",
"httpVersion": "HTTP/1.1",
"queryString": [
{
"name": "foo",
"value": "bar"
},
{
"name": "foo",
"value": "baz"
},
{
"name": "baz",
"value": "abc"
}
],
"headers": [
{
"name": "accept",
"value": "application/json"
},
{
"name": "content-type",
"value": "application/x-www-form-urlencoded"
}
],
"cookies": [
{
"name": "foo",
"value": "bar"
},
{
"name": "bar",
"value": "baz"
}
],
"postData": {
"mimeType": "application/x-www-form-urlencoded",
"params": [
{
"name": "foo",
"value": "bar"
}
]
}
}
```

</details>

```shell
httpsnippet example.json --target shell --client curl --output ./examples
```

```console
$ tree examples
examples/
└── example.sh
```

inside `examples/example.sh` you'll see the generated output:

```shell
curl --request POST \
--url 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value' \
--header 'accept: application/json' \
--header 'content-type: application/x-www-form-urlencoded' \
--cookie 'foo=bar; bar=baz' \
--data foo=bar
```

provide extra options:

```shell
httpsnippet example.json --target shell --client curl --output ./examples --options '{ "indent": false }'
```

and see how the output changes, in this case without indentation

```shell
curl --request POST --url 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value' --header 'accept: application/json' --header 'content-type: application/x-www-form-urlencoded' --cookie 'foo=bar; bar=baz' --data foo=bar
```

## TypeScript Library Usage

### Library Installation

| NPM | Yarn |
| ----------------------------------------- | ------------------------------- |
| <pre>npm install --save httpsnippet</pre> | <pre>yarn add httpsnippet</pre> |
| NPM | Yarn |
| ---------------------------------------------- | ------------------------------------ |
| <pre>npm install --save httpsnippet-lite</pre> | <pre>yarn add httpsnippet-lite</pre> |

### Types

Expand Down Expand Up @@ -292,7 +163,7 @@ interface Target {
Name of [conversion target](https://github.com/Kong/httpsnippet/wiki/Targets)

```ts
import { HTTPSnippet } from 'httpsnippet';
import { HTTPSnippet } from 'httpsnippet-lite';

const snippet = new HTTPSnippet({
method: 'GET',
Expand All @@ -317,11 +188,11 @@ const snippet = new HTTPSnippet({
});

// generate Node.js: Native output
console.log(snippet.convert('node'));
console.log(await snippet.convert('node'));

// generate Node.js: Native output, indent with tabs
console.log(
snippet.convert('node', {
await snippet.convert('node', {
indent: '\t',
}),
);
Expand All @@ -337,7 +208,7 @@ const isTarget: (target: Target) => target is Target;

```ts
import { myCustomTarget } from './my-custom-target';
import { isTarget } from 'httpsnippet';
import { isTarget } from 'httpsnippet-lite';

try {
console.log(isTarget(myCustomTarget));
Expand All @@ -357,12 +228,12 @@ const addTarget: (target: Target) => void;
```ts
import { myCustomClient } from './my-custom-client';
import { HAR } from 'my-custom-har';
import { HTTPSnippet, addTargetClient } from 'httpsnippet';
import { HTTPSnippet, addTargetClient } from 'httpsnippet-lite';

addTargetClient(myCustomClient);

const snippet = new HTTPSnippet(HAR);
const output = snippet.convert('customTargetId');
const output = await snippet.convert('customTargetId');
console.log(output);
```

Expand Down Expand Up @@ -393,34 +264,27 @@ Use `addTargetClient` to add a custom client to an existing target. See [`addTar
const addTargetClient: (targetId: TargetId, client: Client) => void;
```

```ts
import { myCustomClient } from './my-custom-client';
import { HAR } from 'my-custom-har';
import { HTTPSnippet, addTargetClient } from 'httpsnippet';

addTargetClient('customTargetId', myCustomClient);

const snippet = new HTTPSnippet(HAR);
const output = snippet.convert('customTargetId', 'customClientId');
console.log(output);
```
## Documentation

## Bugs and feature requests
At the heart of this module is the [HAR Format](http://www.softwareishard.com/blog/har-12-spec/#request) as the HTTP request description format, please review some of the sample JSON HAR Request objects in [test fixtures](/test/fixtures/requests), or read the [HAR Docs](http://www.softwareishard.com/blog/har-12-spec/#request) for more details.

Have a bug or a feature request? Please first read the [issue guidelines](CONTRIBUTING.md#using-the-issue-tracker) and search for existing and closed issues. If your problem or idea is not addressed yet, [please open a new issue](/issues).
For detailed information on each target, please review the [wiki](https://github.com/Kong/httpsnippet/wiki).

## Contributing
## Differences from `kong/httpsnippet`

Please read through our [contributing guidelines](CONTRIBUTING.md). Included are directions for opening issues, coding standards, and notes on development.
Here's a list of the most significant differences between httpsnippet-lite and [httpsnippet](https://github.com/Kong/httpsnippet) upstream:

For info on creating new conversion targets, please review this [guideline](https://github.com/Kong/httpsnippet/wiki/Creating-Targets)
- No reliance on Node.js core modules and globals
- convert() method is async
- HAR is not validated
- CLI is not bundled
- Dual packaging available

Moreover, if your pull request contains TypeScript patches or features, you must include relevant unit tests.
## License

Editor preferences are available in the [editor config](.editorconfig) for easy use in common text editors. Read more and download plugins at <http://editorconfig.org>.
[MIT](LICENSE) &copy; [Kong](https://konghq.com)

[license-url]: https://github.com/Kong/httpsnippet/blob/master/LICENSE
[npm-url]: https://www.npmjs.com/package/httpsnippet
[npm-license]: https://img.shields.io/npm/l/httpsnippet.svg?style=flat-square
[npm-version]: https://img.shields.io/npm/v/httpsnippet.svg?style=flat-square
[npm-downloads]: https://img.shields.io/npm/dm/httpsnippet.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/httpsnippet-lite
[npm-license]: https://img.shields.io/npm/l/httpsnippet-lite.svg?style=flat-square
[npm-version]: https://img.shields.io/npm/v/httpsnippet-lite.svg?style=flat-square
2 changes: 0 additions & 2 deletions bin/httpsnippet

This file was deleted.

28 changes: 16 additions & 12 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/** @type { import('@jest/types').Config.InitialOptions } */
module.exports = {
export default {
collectCoverage: false,
globals: {
'ts-jest': {
isolatedModules: true,
},
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
injectGlobals: false,
modulePathIgnorePatterns: ['<rootDir>/dist'],
resetMocks: true,
resetModules: true,
testEnvironment: 'node',
testRegex: ['.+\\.test\\.tsx?$'],
transform: { '^.+\\.tsx?$': 'ts-jest' },
// verbose: true,
testRegex: ['.+\\.test\\.ts$'],
preset: 'ts-jest/presets/default-esm',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.ts$': [
'ts-jest',
{
useESM: true,
isolatedModules: true,
},
],
},
};
Loading