Skip to content

Commit

Permalink
Merge pull request #158 from janhommes/feature/2.0.0-rc
Browse files Browse the repository at this point in the history
Feature/2.0.0 rc
  • Loading branch information
janhommes authored Sep 22, 2023
2 parents 0065963 + 63f2899 commit b19afcd
Show file tree
Hide file tree
Showing 11 changed files with 5,481 additions and 4,201 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@ You can pass as a second option into the `o` constructor options. The signature
```typescript
function o(rootUrl: string | URL, config?: OdataConfig | any)
```
The `rootUrl` can be used to directly query a resource:
```javascript
o('http://my.url/some-resource').query().then();
```
But mostly better is to create a handler with a `rootUrl` in the configuration. Then you are able to use the handler multiple times:
```javascript
const oHandler = o('', { rootUrl: 'http://my.url' });
// requesting http://my.url/some-resource
oHandler.get('some-resource').query().then();
```
When creating a oHandler with a configured `rootUrl` in config and as first property, the two are getting merged:
```javascript
const oHandler = o('v1', { rootUrl: 'http://my.url' });
// requesting http://my.url/v1/some-resource
oHandler.get('some-resource').query().then();
```
In a browser you can also use only a resource and the `rootUrl` tries pointing to the current browser:
```javascript
const oHandler = o('v1');
// requesting http://current-url/v1/some-resource
oHandler.get('some-resource').query().then();
```
Basic configuration is based on [RequestInit](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request) and additional [odata config](src/OdataConfig.ts). By default o.js sets the following values:
```
{
Expand Down Expand Up @@ -138,6 +167,33 @@ Basic configuration is based on [RequestInit](https://developer.mozilla.org/en-U
```
## Query
> Since version 2.0.0 we support the use of [odata-query](https://www.npmjs.com/package/odata-query). You can simply add a
`buildQuery` property to any `query()` and `fetch()` request (if only used as filter):
```typescript
import buildQuery from 'odata-query'
const filter = {
not: {
and:[
{ SomeProp: 1 },
{ AnotherProp: 2 }
]
}
};
// using only filter in query() or fetch():
oHandler.get('People')
.query(buildQuery({ filter }))
.then((filteredPeople) => {});
// using more features of odata-query in get:
oHandler.get('People' + buildQuery({ filter, key: 1, top: 10 }))
.query()
.then((filteredPeople) => {});
``
The following query options are supported by `query()`, `fetch()` and `batch()` by simply adding them as object:
```typescript
Expand Down Expand Up @@ -170,4 +226,4 @@ The lib tries to parse the data on each request. Sometimes that is not wanted (e
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 node.js. If you like polyfills to support IE11 please include the `dist/umd/o.polyfill.js` file.
If you like polyfills to support IE11 please include the `dist/umd/o.polyfill.js` file. Version < 2 adds polyfills for node automatically. Version 2.0.0 and bigger only supports node 18 and higher where fetch and URL is included.
7 changes: 4 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ <h2 data-bind="text:LastName"></h4>
</div>

<footer class="footer">
<p>&copy; Company 2020</p>
<p>&copy; Company 2023</p>
</footer>

</div> <!-- /container -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/q.js"></script>
<script src="https://unpkg.com/[email protected]/dist/umd/o.polyfill.js"></script>
<script src="https://unpkg.com/[email protected]/dist/umd/o.min.js"></script>
<!--<script src="https://unpkg.com/[email protected]/dist/umd/o.polyfill.js"></script>-->
<script src="https://unpkg.com/[email protected]/dist/umd/o.min.js"></script>
<!--<script src="../dist/umd/o.min.js"></script>-->
<script src="js/knockout-3.2.0.js"></script>
<script src="js/ko.bindinghandler.currency.js"></script>
<script src="browser.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion example/node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const odata = require('odata');
const odata = require('../dist/cjs/o.js');


const oHandler = odata.o('https://services.odata.org/V4/%28S%28wptr35qf3bz4kb5oatn432ul%29%29/TripPinServiceRW/', {
Expand Down
5 changes: 4 additions & 1 deletion global-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ const fetchPolyfill = require('whatwg-fetch');
global.fetch = fetchPolyfill.fetch;
global.Request = fetchPolyfill.Request;
global.Headers = fetchPolyfill.Headers;
global.Response = fetchPolyfill.Response;
global.Response = fetchPolyfill.Response;

// let's simulate browser for tests.
window = {};
Loading

0 comments on commit b19afcd

Please sign in to comment.