Comparing version 0.1.0 to 0.1.1
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var HTTP_METHOD_1 = require("../enum/HTTP_METHOD"); | ||
var isEmpty_1 = __importDefault(require("../lib/isEmpty")); | ||
var isEmpty_1 = require("../lib/isEmpty"); | ||
var AxiosRequestConfigAdapter = /** @class */ (function () { | ||
@@ -14,3 +11,3 @@ function AxiosRequestConfigAdapter(_prop) { | ||
get: function () { | ||
if (isEmpty_1.default(this._prop.method)) { | ||
if (isEmpty_1.isEmpty(this._prop.method)) { | ||
return HTTP_METHOD_1.HTTP_METHOD.GET; | ||
@@ -48,3 +45,3 @@ } | ||
get: function () { | ||
if (isEmpty_1.default(this._prop.headers)) { | ||
if (isEmpty_1.isEmpty(this._prop.headers)) { | ||
return {}; | ||
@@ -51,0 +48,0 @@ } |
@@ -0,5 +1,7 @@ | ||
import { HEADER_CONTENT_TYPE } from '../enum/HEADER_CONTENT_TYPE'; | ||
export interface IR2CurlOptions { | ||
/** Determines the type of quota around the body and uri. */ | ||
quote: 'single' | 'double'; | ||
defaultContentType: HEADER_CONTENT_TYPE | string | false; | ||
} | ||
export declare const defaultR2CurlOptions: IR2CurlOptions; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var HEADER_CONTENT_TYPE_1 = require("../enum/HEADER_CONTENT_TYPE"); | ||
exports.defaultR2CurlOptions = { | ||
quote: 'single', | ||
defaultContentType: HEADER_CONTENT_TYPE_1.HEADER_CONTENT_TYPE.JSON_UTF8, | ||
}; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isEmpty_1 = __importDefault(require("./isEmpty")); | ||
var isEmpty_1 = require("./isEmpty"); | ||
var BodyHelper = /** @class */ (function () { | ||
@@ -16,3 +13,3 @@ function BodyHelper(_headers, _rawBody) { | ||
BodyHelper.prototype.toString = function () { | ||
if (isEmpty_1.default(this.body)) { | ||
if (isEmpty_1.isEmpty(this.body)) { | ||
return ''; | ||
@@ -23,3 +20,3 @@ } | ||
BodyHelper.prototype.getContentType = function () { | ||
if (isEmpty_1.default(this._headers)) { | ||
if (isEmpty_1.isEmpty(this._headers)) { | ||
return null; | ||
@@ -29,3 +26,3 @@ } | ||
var contentTypePair = lowerHeaderArray.filter(function (header) { return header[0].toLowerCase() === 'content-type'; })[0]; | ||
if (isEmpty_1.default(contentTypePair)) { | ||
if (isEmpty_1.isEmpty(contentTypePair)) { | ||
return null; | ||
@@ -36,8 +33,8 @@ } | ||
BodyHelper.prototype.parseBody = function () { | ||
if (isEmpty_1.default(this._rawBody)) { | ||
if (isEmpty_1.isEmpty(this._rawBody)) { | ||
return null; | ||
} | ||
if (!isEmpty_1.default(this.contentType) && | ||
if (isEmpty_1.isNotEmpty(this.contentType) && | ||
this.contentType.includes('application/x-www-form-urlencoded') && | ||
!isEmpty_1.default(this._rawBody) && | ||
isEmpty_1.isNotEmpty(this._rawBody) && | ||
typeof this._rawBody === 'object') { | ||
@@ -44,0 +41,0 @@ return this.getFormBody(); |
@@ -5,4 +5,5 @@ import { IR2CurlOptions } from '../interface/IR2CurlOptions'; | ||
private readonly _adap; | ||
private readonly _option; | ||
private readonly outputQuote; | ||
constructor(_adap: IRequestAdaptor, option: IR2CurlOptions); | ||
constructor(_adap: IRequestAdaptor, _option: IR2CurlOptions); | ||
readonly method: string; | ||
@@ -9,0 +10,0 @@ readonly headers: string; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var BodyHelper_1 = require("./BodyHelper"); | ||
var isEmpty_1 = __importDefault(require("./isEmpty")); | ||
var HeaderHelper_1 = require("./HeaderHelper"); | ||
var isEmpty_1 = require("./isEmpty"); | ||
var CurlBuilder = /** @class */ (function () { | ||
function CurlBuilder(_adap, option) { | ||
function CurlBuilder(_adap, _option) { | ||
this._adap = _adap; | ||
this.outputQuote = option.quote === 'single' ? '\'' : '"'; | ||
this._option = _option; | ||
this.outputQuote = _option.quote === 'single' ? '\'' : '"'; | ||
} | ||
Object.defineProperty(CurlBuilder.prototype, "method", { | ||
get: function () { | ||
if (isEmpty_1.default(this._adap.method)) { | ||
if (isEmpty_1.isEmpty(this._adap.method)) { | ||
return ''; | ||
@@ -26,6 +25,8 @@ } | ||
var _this = this; | ||
if (isEmpty_1.default(this._adap.headers)) { | ||
var helper = new HeaderHelper_1.HeaderHelper(this._adap.headers, this._adap.method, this._option); | ||
var headers = helper.toObject(); | ||
if (isEmpty_1.isEmpty(headers)) { | ||
return ''; | ||
} | ||
return Object.entries(this._adap.headers) | ||
return Object.entries(headers) | ||
.map(function (header) { return "-H " + _this.wrapQuote(header[0] + ":" + header[1]); }) | ||
@@ -41,3 +42,3 @@ .join(' '); | ||
var body = helper.toString(); | ||
if (isEmpty_1.default(body)) { | ||
if (isEmpty_1.isEmpty(body)) { | ||
return ''; | ||
@@ -58,3 +59,3 @@ } | ||
CurlBuilder.prototype.toString = function () { | ||
var existData = [this.method, this.url, this.headers, this.body].filter(function (data) { return !isEmpty_1.default(data); }); | ||
var existData = [this.method, this.url, this.headers, this.body].filter(function (data) { return !isEmpty_1.isEmpty(data); }); | ||
return ("curl " + existData.join(' ')).trim(); | ||
@@ -61,0 +62,0 @@ }; |
@@ -1,1 +0,2 @@ | ||
export default function isEmpty(value: any): value is null | undefined; | ||
export declare function isEmpty<T>(value: T | any): value is null | undefined; | ||
export declare function isNotEmpty<T>(value: T | any): value is T; |
@@ -21,2 +21,6 @@ "use strict"; | ||
} | ||
exports.default = isEmpty; | ||
exports.isEmpty = isEmpty; | ||
function isNotEmpty(value) { | ||
return !isEmpty(value); | ||
} | ||
exports.isNotEmpty = isNotEmpty; |
{ | ||
"name": "r2curl", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Node.js Request Wrapper (axios, fetch, ..) to cURL Command String", | ||
@@ -13,2 +13,3 @@ "main": "dist/index.js", | ||
"test:log": "npx cross-env DEBUG=r2curl:tc:* npx jest --coverage", | ||
"test:watch": "npx cross-env DEBUG=r2curl:tc:* npx jest --watch", | ||
"lint": "npx tslint -c ./tslint.json \"./src/**/*.ts\"", | ||
@@ -15,0 +16,0 @@ "clean": "npm run clean:dist && npm run clean:artifact", |
# r2curl | ||
[![npm version](https://badge.fury.io/js/r2curl.svg)](https://badge.fury.io/js/r2curl) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) [![CircleCI](https://circleci.com/gh/uyu423/r2curl.svg?style=svg)](https://circleci.com/gh/uyu423/r2curl) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/c6b96d8b0ded4763aaf8b6a8179fb093)](https://app.codacy.com/app/uyu423/r2curl?utm_source=github.com&utm_medium=referral&utm_content=uyu423/r2curl&utm_campaign=Badge_Grade_Dashboard) [![Code Climate](https://codeclimate.com/github/uyu423/r2curl.svg)](https://codeclimate.com/github/uyu423/r2curl) [![Test Coverage](https://api.codeclimate.com/v1/badges/bb19fbd2394b545aefb2/test_coverage)](https://codeclimate.com/github/uyu423/r2curl/test_coverage) | ||
[![npm version](https://badge.fury.io/js/r2curl.svg)](https://badge.fury.io/js/r2curl) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) [![CircleCI](https://circleci.com/gh/uyu423/r2curl.svg?style=svg)](https://circleci.com/gh/uyu423/r2curl) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/f74cdea970d44550a0bff9319e467256)](https://www.codacy.com/app/uyu423/r2curl?utm_source=github.com&utm_medium=referral&utm_content=uyu423/r2curl&utm_campaign=Badge_Grade)[![Code Climate](https://codeclimate.com/github/uyu423/r2curl.svg)](https://codeclimate.com/github/uyu423/r2curl) [![Test Coverage](https://api.codeclimate.com/v1/badges/bb19fbd2394b545aefb2/test_coverage)](https://codeclimate.com/github/uyu423/r2curl/test_coverage) | ||
Node.js Request Wrapper (axios, fetch, ..) to cURL Command String | ||
@@ -12,8 +10,8 @@ | ||
- [x] axios | ||
- [x] AxiosRequestConfig | ||
- [x] AxiosResposne | ||
- [ ] node-fetch | ||
- [ ] request | ||
- [ ] ... | ||
- [x] axios | ||
- [x] AxiosRequestConfig | ||
- [x] AxiosResposne | ||
- [ ] node-fetch | ||
- [ ] request | ||
- [ ] ... | ||
@@ -62,4 +60,9 @@ ## Usage | ||
### More Options | ||
### More `r2curl` Options | ||
#### `option.quote` | ||
- Determines the type of quota around the body and uri. | ||
- default is `single` | ||
```typescript | ||
@@ -77,1 +80,24 @@ import r2curl from 'r2curl'; | ||
``` | ||
#### `option.defaultContentType` | ||
- Determines the default Content-Type header value for `POST` and `PUT` requests. | ||
- default is `application/json; charset=utf-8` | ||
- Type is `(enum) HEADER_CONTENT_TYPE` | `string` | `false`; | ||
- If you give `(boolean) false` to `defaultContentType`, you can disable `Content-Type` Header. | ||
```typescript | ||
import r2curl, { HEADER_CONTENT_TYPE } from 'r2curl'; | ||
// const optionUsingEnum = { | ||
// defaultContentType: HEADER_CONTENT_TYPE.TEXT, | ||
// }; | ||
const option = { | ||
defaultContentType: 'application/json5', | ||
} | ||
const request: AxiosRequestConfig = { url: 'https://google.com', method: 'POST' }; | ||
const curl = r2curl(config, option); | ||
console.log(curl); | ||
// output: curl -X POST 'https://google.com' -H 'Content-Type:application/json5 | ||
``` |
21406
23
424
100