Comparing version
{ | ||
"name": "odata", | ||
"description": "o.js is a isomorphic Odata Javascript library to simplify the request of data. The main goal is to build a standalone, lightweight and easy to understand Odata lib.", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"main": "dist/cjs/o.js", | ||
@@ -9,2 +9,5 @@ "browser": "dist/umd/o.js", | ||
"types": "dist/types/o.d.ts", | ||
"engines": { | ||
"node": ">=10.0" | ||
}, | ||
"dependencies": { | ||
@@ -27,3 +30,3 @@ "cross-fetch": "^3.0.0", | ||
"rollup-plugin-uglify-es": "0.0.1", | ||
"ts-jest": "^23.10.5", | ||
"ts-jest": "^26.1.0", | ||
"tslint": "^5.12.1", | ||
@@ -30,0 +33,0 @@ "typescript": "^3.2.2", |
@@ -50,3 +50,3 @@ # o.js | ||
```javascript | ||
const o = require('odata'); | ||
const o = require('odata').o; | ||
@@ -118,2 +118,3 @@ // promise example | ||
batch: { | ||
boundaryPrefix: "batch_", | ||
changsetBoundaryPrefix: "changset_", | ||
@@ -126,3 +127,2 @@ endpoint: "$batch", | ||
}, | ||
boundaryPrefix: "batch_", | ||
credentials: "omit", | ||
@@ -160,3 +160,8 @@ fragment: "value", | ||
``` | ||
> The $count flag will add an inline count property as metadata to a query response. In order to just retrieve the count, you'll have query the $count resource, such as | ||
```typescript | ||
oHandler.get('People/$count').query().then((count) => {}) | ||
``` | ||
The queries are always attached as the [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams). | ||
@@ -168,5 +173,5 @@ | ||
## Batching | ||
By default o.js request chained request in sequent. You can batch them together by using `batch()`. They are then send to the defined batch endpoint in the config. Changsets are at the moment in a experimental phase and needs to be enabled in the config. | ||
By default o.js chains request in sequent. You can batch them together by using `batch()`. They are then send to the defined batch endpoint in the config. Changsets are at the moment in a experimental phase and needs to be enabled in the config. | ||
## Polyfills | ||
Polyfills are automatically added for `fetch()` and `URL()` if needed. The automatic polyfilling can be disabled in the options. |
@@ -37,2 +37,3 @@ import { OdataConfig } from "./OdataConfig"; | ||
batch: { | ||
boundaryPrefix: "batch_", | ||
changsetBoundaryPrefix: "changset_", | ||
@@ -45,3 +46,2 @@ endpoint: "$batch", | ||
}, | ||
boundaryPrefix: "batch_", | ||
credentials: "omit", | ||
@@ -48,0 +48,0 @@ fragment: "value", |
@@ -5,4 +5,7 @@ import { OdataConfig } from "./OdataConfig"; | ||
const CRLF = "\r\n"; | ||
export class OBatch { | ||
private batchBody: string; | ||
// "" here prevents 'undefined' at start of body under some conditions. | ||
private batchBody = ""; | ||
private batchUid; | ||
@@ -21,3 +24,3 @@ private batchConfig: OdataConfig; | ||
"Content-Type", | ||
`multipart/mixed; boundary=${this.batchUid}`, | ||
`multipart/mixed;boundary=${this.batchUid}`, | ||
); | ||
@@ -37,16 +40,15 @@ | ||
contentId++; | ||
return ` | ||
Content-Type: application/http | ||
Content-Transfer-Encoding: binary | ||
Content-ID: ${contentId} | ||
return [ | ||
"", | ||
"Content-Type: application/http", | ||
"Content-Transfer-Encoding: binary", | ||
`Content-ID: ${contentId}`, | ||
"", | ||
`${req.config.method} ${req.url.href} HTTP/1.1`, | ||
`${this.getHeaders(req)}`, | ||
`${this.getBody(req)}` | ||
].join(CRLF); | ||
}).join(`${CRLF}--${this.batchUid}`); | ||
${req.config.method} ${req.url.href} HTTP/1.1 | ||
${this.getHeaders(req)} | ||
${this.getBody(req)}`; | ||
}).join(` | ||
--${this.batchUid}`); | ||
this.batchBody += ` | ||
--${this.batchUid}-- | ||
`; | ||
this.batchBody += `${CRLF}--${this.batchUid}--${CRLF}`; | ||
} | ||
@@ -111,6 +113,8 @@ | ||
if (this.changeset) { | ||
this.batchBody += ` | ||
Content-Type: multipart/mixed; boundary=${this.batchUid} | ||
--${this.batchUid}`; | ||
this.batchBody += [ | ||
"", | ||
`Content-Type: multipart/mixed;boundary=${this.batchUid}`, | ||
"", | ||
`--${this.batchUid}` | ||
].join(CRLF); | ||
} else if (changeRes.length > 0) { | ||
@@ -141,6 +145,3 @@ this.batchBody = `--${this.batchUid}`; | ||
if (req.config.body) { | ||
return ` | ||
${req.config.body} | ||
`; | ||
return `${req.config.body}${CRLF}${CRLF}`; | ||
} | ||
@@ -164,7 +165,17 @@ return ""; | ||
private getHeaders(req: ORequest) { | ||
return Object.keys(req.config.headers) | ||
.map((name) => `${name}:${req.config.headers[name]}`) | ||
.join("\n"); | ||
private getHeaders(req: ORequest): string { | ||
// Request headers can be Headers | string[][] | Record<string, string>. | ||
// A new Headers instance around them allows treatment of all three types | ||
// to be the same. This also applies security last two could bypass. | ||
const headers = new Headers(req.config.headers || undefined) as any; | ||
// Convert each header to single string. | ||
// Headers is iterable. Array.from is needed instead of Object.keys. | ||
const mapped = Array.from(headers).map(([k, v]) => `${k}: ${v}`); | ||
if (mapped.length) { | ||
// Need to ensure a blank line between HEADERS and BODY. When there are | ||
// headers, it must be added here. Otherwise blank is added in ctor. | ||
mapped.push(""); | ||
} | ||
return mapped.join(CRLF); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1321561
0.09%14111
0.09%173
2.98%