Comparing version 0.0.2-beta.1 to 0.0.3
{ | ||
"devDependencies": { | ||
"parcel": "^1.12.4" | ||
}, | ||
"name": "irajs", | ||
"version": "0.0.2-beta.1", | ||
"version": "0.0.3", | ||
"description": "Ira Fetch - Vanilla JS Fetch API wrapper with goodies 🍒", | ||
@@ -8,0 +5,0 @@ "main": "src/index.js", |
@@ -9,3 +9,3 @@ # Ira Fetch Wrapper | ||
# Install | ||
## NPM Install | ||
@@ -16,2 +16,10 @@ ``` | ||
## CDN | ||
```html | ||
<script src="https://d3portillo.github.io/ira/src/index.js"></script> | ||
``` | ||
Long live to Github Pages : ) | ||
## Examples | ||
@@ -65,3 +73,3 @@ | ||
- Set *debug* mode | ||
- Set _debug_ mode | ||
@@ -74,3 +82,3 @@ ```js | ||
debug: true, | ||
parseBlob: false /* Do not include .blob body response */ | ||
parseBlob: false /* Do not include .blob body response */, | ||
}) | ||
@@ -82,5 +90,84 @@ ``` | ||
```js | ||
nfetch.get("https://something").then(({ blob })=>console.info(null==blob)) | ||
nfetch.get("https://something").then(({ blob }) => console.info(null == blob)) | ||
``` | ||
- Fetching with keys and session that contain same config | ||
```js | ||
const request = ira.extend({ | ||
headers: { | ||
"x-api-key": "somsaltedencryptedawesomekey", | ||
}, | ||
debug: true /* Show Ira stuff on console */, | ||
baseURL: "https://someendpoint" | ||
parseBlob: false /* Do not include .blob body response */ | ||
}) | ||
request.get("/stuff").then(({ data })=> console.log({ data })) | ||
request.get("/post", { headers: { "a-header": "a-value" } }).then(({ data })=>{ | ||
console.log({data}) | ||
}) | ||
``` | ||
## Ira Object instances | ||
```js | ||
IRA_RESPONSE = { | ||
data: { json: Object, text: String, blob: ?Blob } | ||
ok: Boolean, | ||
status: Number, | ||
statusText: String, | ||
statusCode: status<Number>, | ||
error:?Error | ||
} | ||
IRA_REQUEST_METHOD_PROPS = { | ||
headers: {}, | ||
body: ?String, | ||
...({ | ||
Request:`https://developer.mozilla.org/en-US/docs/Web/API/Request` | ||
}) | ||
} | ||
IRA_SETTINGS = { | ||
headers: {}, | ||
debug: Boolean, | ||
parseBlob: Boolean, | ||
baseURL: ?String, | ||
} | ||
IRA_HTTP_METHODS = { | ||
get: Function, | ||
put: Function, | ||
post: Function, | ||
head: Function, | ||
delete: Function, | ||
connect: Function, | ||
options: Function, | ||
trace: Function, | ||
} | ||
// Exported object {Main} | ||
ira = { | ||
...IRA_HTTP_METHODS, | ||
default(): IRA_HTTP_METHODS.get, | ||
_settings: Object, | ||
reset: Function, | ||
config: Function, | ||
extend: IRA_HTTP_METHODS | ||
} | ||
``` | ||
| Name/Instance | Params ? | Returns | Comments | | ||
| ---------------- | ---------------------------------- | ----------------------- | -------------------------------------------------------------------------------- | | ||
| IRA_HTTP_METHODS | (`URL`,`IRA_REQUEST_METHOD_PROPS`) | `Promise<IRA_RESPONSE>` | Fetch API HTTP Methods | | ||
| default() | (`URL`,`IRA_REQUEST_METHOD_PROPS`) | `Promise<IRA_RESPONSE>` | When you do Ira("URL") | | ||
| \_settings | `NONE` | `Void` | Acces current Ira global settings | | ||
| reset() | `NONE` | `Void` | Resets persistence settings to default | | ||
| config() | `IRA_SETTINGS` | `Void` | Set ira settings (This affects all requests on main ira Object, not forked ones) | | ||
| extend() | `IRA_SETTINGS` | `IRA_HTTP_METHODS` | Returns a fork of Ira with just HTTP Methods and provided Ira Settings | | ||
> Ira stands for: Go To, rage or anger. That's all the feelings you have while handling HTTP stuff : ) | ||
## TODO | ||
- [ ] Unit tests | ||
- [ ] Some custom examples | ||
- [ ] Live examples on Codepen or similar |
@@ -12,2 +12,3 @@ /** | ||
parseBlob: true, | ||
baseURL: undefined, | ||
} | ||
@@ -31,3 +32,3 @@ const IRA_METHODS = { | ||
// * Ira config main settings object | ||
// * User Ira config object | ||
var persistentIraConfig = { ...INIT_IRA_CONFIG } | ||
@@ -38,6 +39,6 @@ const makeIraFetch = (method = "GET", options = { acceptsBody: true }) => { | ||
* @typedef {Object} IraResponse | ||
* @property {{ json: Object, text: string, blob: ?Blob }} data - Posible parsed response body | ||
* @property {{ json: Object, text: String, blob: ?Blob }} data - Posible parsed response body | ||
* @property {Boolean} ok - Response status <= 300 | ||
* @property {number} status | ||
* @property {string} statusText | ||
* @property {String} statusText | ||
* @property {number} statusCode | ||
@@ -48,4 +49,5 @@ * @property {?Error} error - Null if nothing wrong | ||
/** | ||
* @param {string} url - URL To fetch from | ||
* @param {{ headers: {}, body: ?string }} extra - Your normal fetch opts | ||
* @param {String} url - URL To fetch from | ||
* @param {{ headers: {}, body: ?String }} extra - Your normal fetch opts | ||
* @param {INIT_IRA_CONFIG} config - Custom Ira config | ||
* @return {Promise<IraResponse>} | ||
@@ -76,2 +78,4 @@ */ | ||
try { | ||
const { baseURL, parseBlob } = config | ||
url = baseURL ? `${baseURL}${url}` : url | ||
const { fetch } = window | ||
@@ -92,3 +96,3 @@ if (!fetch) throw new Error("Not inside a browser") | ||
response.text(), | ||
config.parseBlob ? clone.blob() : null, | ||
parseBlob ? clone.blob() : null, | ||
]).then(([a, b]) => { | ||
@@ -117,3 +121,3 @@ // * [a] must be string | ||
if (config.debug) { | ||
console.info(`${IRA} REQ_URL='${url}' >>> FULL_REQ_DATA: `, { | ||
console.info(`${IRA} REQ_URL='${url}' >>> REQ_DATA: `, { | ||
headers, | ||
@@ -123,3 +127,2 @@ body, | ||
extra, | ||
response, | ||
}) | ||
@@ -130,4 +133,5 @@ } | ||
.catch((error) => { | ||
const statusCode = 500 // Will need to figure out how to get status | ||
console.error( | ||
`${IRA}, got error on request. REQ_URL='${url}' >>> FULL_REQ_DATA: `, | ||
`${IRA} - Got error on request, REQ_URL='${url}' >>> REQ_DATA: `, | ||
{ | ||
@@ -141,5 +145,5 @@ error, | ||
send({ | ||
data: { json: {}, text: "", blob: null }, | ||
data: { json: {}, text: "", blob: null } /* Void data */, | ||
ok: false, | ||
status, | ||
status: statusCode, | ||
statusText: error, | ||
@@ -219,7 +223,7 @@ statusCode: 500, | ||
/** Your custom settings Ira fork | ||
* @param {string} url | ||
* @param {{ headers: {}, body: ?string }} extra | ||
* @param {String} url | ||
* @param {{ headers: {}, body: ?String }} extra | ||
*/ | ||
const defaultFunction = (url, extra) => null | ||
const myMethodsInterface = { | ||
const myMethods = { | ||
get: defaultFunction, | ||
@@ -234,11 +238,13 @@ put: defaultFunction, | ||
} | ||
Object.keys(methods).map((name) => { | ||
const f = makeIraFetch(name.toUpperCase(), { | ||
acceptsBody: !IRA_METHODS_WITHOUT_BODY.includes(name), | ||
Object.keys(IRA_METHODS).map((name) => { | ||
const METHOD = name.toUpperCase() | ||
const acceptsBody = !IRA_METHODS_WITHOUT_BODY.includes(METHOD) | ||
const f = makeIraFetch(METHOD, { | ||
acceptsBody, | ||
}) | ||
myMethodsInterface[name] = (u, o) => f(u, o, config) | ||
myMethods[name] = (u, o) => f(u, o, config) | ||
}) | ||
// Extend returns methods constrained to user config | ||
return myMethodsInterface | ||
return myMethods | ||
} | ||
@@ -245,0 +251,0 @@ |
15
test.js
@@ -1,3 +0,1 @@ | ||
import ira from "./src" | ||
console.info("GET METHOD") | ||
@@ -17,1 +15,14 @@ ira.get(`https://postman-echo.com/get?foo1=bar1&foo2=bar2`).then(({ data }) => { | ||
}) | ||
console.info("GET METHOD With custom headers") | ||
const request = ira.extend({ | ||
headers: { | ||
"x-api-key": "BOg81b54cfD99ufEmd21sxgp696bm7XcT2F2jVOzdw21", | ||
session: "LsZoeYLx2cAwMsauPsP56nWgMpu3T89Jy", | ||
}, | ||
debug: true, | ||
label: "Request with custom headers", | ||
baseURL: "https://documenter.getpostman.com/view/3632562377", | ||
}) | ||
request.get("/something") |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12939
0
257
168