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

List name should be capitalized #issue18 #20

Closed
wants to merge 18 commits into from
Closed
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
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/interface-name-prefix": "off",
"quotes": [2, "single"],
"@typescript-eslint/explicit-module-boundary-types": "off"
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ test/integration/config.ts
*.js
*.js.map
!gulpfile.js
!build/**
!build/**
!.eslintrc.js
.nyc_output/
coverage/
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ test
/typings
.vscode
*.js.map
!/lib
/coverage
/.nyc_output
!/lib
94 changes: 38 additions & 56 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
{
"version": "0.2.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
{
"request": "launch",
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Run mocha",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": [
"${workspaceRoot}\\lib\\test\\unit\\tests.js"
],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": "${workspaceRoot}",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
"sourceMaps": true,
"outDir": "${workspaceRoot}/lib",
// Environment variables passed to the program.
"env": {
"NODE_ENV": "development"
}
},
{
"request": "launch",
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Run mocha integration",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": [
"${workspaceRoot}\\lib\\test\\integration\\tests.js"
],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": "${workspaceRoot}",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
"sourceMaps": true,
"outDir": "${workspaceRoot}/lib",
// Environment variables passed to the program.
"env": {
"NODE_ENV": "development"
}
}
]
}
"version": "0.2.0",
"configurations": [
{
"name": "Run mocha integration",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"-r",
"ts-node/register",
"test/integration/tests.ts"
],/*
"env": {
"http_proxy": "http://127.0.0.1:8888"
},*/
"cwd": "${workspaceRoot}",
"protocol": "inspector"
},
{
"name": "Run mocha unit test",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"-r",
"ts-node/register",
"test/unit/tests.ts"
],/*
"env": {
"http_proxy": "http://127.0.0.1:8888"
},*/
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
39 changes: 27 additions & 12 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
/*
👋 Hi! This file was autogenerated by tslint-to-eslint-config.
https://github.com/typescript-eslint/tslint-to-eslint-config

It represents the closest reasonable ESLint configuration to this
project's original TSLint configuration.

We recommend eventually switching this configuration to extend from
the recommended rulesets in typescript-eslint.
https://github.com/typescript-eslint/tslint-to-eslint-config/blob/master/docs/FAQs.md

Happy linting! 💖
*/
{
"editor.renderWhitespace": true,
"editor.tabSize": 2,
"files.trimTrailingWhitespace": true,
"files.exclude": {
"node_modules/": true,
"**/*.js": { "when": "$(basename).ts"},
"**/*.js.map": true
},
"files.autoSave": "onFocusChange",
"editor.minimap.renderCharacters": false,
"editor.minimap.maxColumn": 90
}
"editor.minimap.maxColumn": 90,
"editor.minimap.renderCharacters": false,
"editor.renderWhitespace": true,
"editor.tabSize": 2,
"files.autoSave": "onFocusChange",
"files.exclude": {
"node_modules/": true,
"**/*.js": {
"when": "$(basename).ts"
},
"**/*.js.map": true
},
"files.trimTrailingWhitespace": true
}
82 changes: 63 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
# sp-request - simplified SharePoint HTTP client
[![NPM](https://nodei.co/npm/sp-request.png?mini=true)](https://nodei.co/npm/sp-request/)

[![Circle CI](https://circleci.com/gh/s-KaiNet/sp-request/tree/master.svg?style=shield&circle-token=c550cd1b34315e710c5e751dd4cffe5cb8e694fe)](https://circleci.com/gh/s-KaiNet/sp-request/tree/master)
[![Coverage Status](https://coveralls.io/repos/github/s-KaiNet/sp-request/badge.svg?branch=master)](https://coveralls.io/github/s-KaiNet/sp-request?branch=master)
[![npm version](https://badge.fury.io/js/sp-request.svg)](https://badge.fury.io/js/sp-request)

### Need help on SharePoint with Node.JS? Join our gitter chat and ask question! [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/sharepoint-node/Lobby)

> If you are looking for a way to perform some REST queries against SharePoint, consider also [PnP-JS-Core](https://github.com/SharePoint/PnP-JS-Core) with [node.js integration](https://github.com/s-KaiNet/node-pnp-js)

`sp-request` based on [request-promise](https://github.com/request/request-promise) (promise-aware implementation of [request](https://github.com/request/request)) and [node-sp-auth](https://github.com/s-KaiNet/node-sp-auth) modules. `node-sp-auth` implements different authentication options for unattended SharePoint authentication from nodejs. You can send REST queries to SharePoint (works with both on-prem and online) using well-known `request` syntax with the same params that `request` supports, and `sp-request` (with help of `node-sp-auth`) takes care about authenticating you inside SharePoint. Responses implemented using modern promise-style approach.
> If you are looking for a way to perform some REST queries against SharePoint, consider also [PnPjs](https://github.com/pnp/pnpjs) with [node.js integration](https://github.com/SharePoint-NodeJS/pnp-auth)
---
> **IMPORTANT:** This module doesn't work in browser. The only supported environment is nodejs. If you have a need to use it in browser, probably you're looking for [sp-rest-proxy](https://github.com/koltyakov/sp-rest-proxy) - a nodejs proxy, which redirects calls to real SharePoint.
---
`sp-request` is based on [got](https://github.com/sindresorhus/got/) (human-friendly and powerful HTTP request library for Node.js) and [node-sp-auth](https://github.com/s-KaiNet/node-sp-auth) modules. `node-sp-auth` implements different authentication options for unattended SharePoint authentication from nodejs. You can send REST queries to SharePoint (works with both on-prem and online) using `got` syntax with the same params that `got` supports, and `sp-request` (with help of `node-sp-auth`) takes care about authenticating you inside SharePoint.

Versions supported:
* SharePoint 2013, 2016
* SharePoint 2013 and onwards
* SharePoint Online

---

## Upgrade from 2.x to 3.x

If you're upgrading to 3.x version, please read [Upgrade guide](/UpgradeTo3x.md)

---

### How to use:

#### Install:

```bash
npm install sp-request --save-dev
```

#### Create sprequest function:

```javascript
import * as sprequest from 'sp-request';
let spr = sprequest.create(credentialOptions);
```

##### Get list by title:

```javascript
spr.get('http://sp2013dev/sites/dev/_api/web/lists/GetByTitle(\'TestList\')')
.then(response => {
Expand All @@ -37,7 +47,9 @@ spr.get('http://sp2013dev/sites/dev/_api/web/lists/GetByTitle(\'TestList\')')
console.log('Ohhh, something went wrong...');
});
```

##### Update list title:

```javascript
spr.requestDigest('http://sp2013dev/sites/dev')
.then(digest => {
Expand Down Expand Up @@ -65,60 +77,90 @@ spr.requestDigest('http://sp2013dev/sites/dev')
}
});
```

... as simple as that! A bit more samples you can find under [integration tests](https://github.com/s-KaiNet/sp-request/blob/master/test/integration/integration.spec.ts)

## API:

### [main sp-request export].create(credentialOptions):
- **_credentialOptions_:** optional, object containing credentials.

- **_credentialOptions_:** optional, object containing credentials.
Since version 2.x `sp-request` relies on `node-sp-auth` module for authentication. You can find description for `credentialOptions` under [node-sp-auth](https://github.com/s-KaiNet/node-sp-auth#params).

Call to `sprequest.create(credentialOption)` returns sprequest function with predefined authentication. You can use this function later to send REST queries (like in samples above) without specifying credentials again.
### sprequest(options):
- **_options_**: required, settings object for `request` module. For all available values refer to the original [request docs](https://github.com/request/request#requestoptions-callback)

By default `sp-request` sets following params for `request`:
- **_options_**: required, settings object for `got` module. For all available values refer to the original [got docs](https://github.com/sindresorhus/got)

By default `sp-request` sets following params for `got`:

```
{
json: true,
strictSSL: false, /* bypassing SSL validation errors */
responseType: 'json',
resolveBodyOnly: false,
rejectUnauthorized: false,
throwHttpErrors: true,
retry: 0,
headers: {
'Accept': 'application/json;odata=verbose',
'Content-Type': 'application/json;odata=verbose'
}
}
```

as a result you can access `body.d` property as an object. You can provide your own headers and override defaults if it's required.
The only difference from original `request`, that `sp-request` returns Bluebird's `Promise` (ES2015 promise implementation), instead of relying on callbacks. So you can combine multiple requests in a convenient and readable way.

### sprequest.requestDigest(url):

- _url_ - required, string site url

Returns request digest as string via promise.

## Convenience methods:

### sprequest(url, options):

- _url_ - required, string
- _options_ - required, `request` options object
- _options_ - optional, `got` options object

The same as `sprequest(options)` but `options.url` will be equal to the first param.

### sprequest.get(url, options)

- _url_ - required, string
- _options_ - optional, `request` options object
- _options_ - optional, `got` options object

The same as `sprequest(options)` but `options.url` will be equal to the first param and `options.method: 'GET'`.

### sprequest.post(url, options)

- _url_ - required, string
- _options_ - optional, `request` options object
- _options_ - optional, `got` options object

The same as `sprequest(options)` but `options.url` will be equal to the first param and `options.method: 'POST'`.

## Supplying additional headers via environment variables
Sometimes you need to push additional headers for `sp-request` without direct access to `sp-requests` object. For example from third party module, which uses `sp-request` internally. For that purpose you can use environmental variables. Provide it in a below way:

Sometimes you need to push additional headers for `sp-request` without direct access to `sp-request` object. For example from third party module, which uses `sp-request` internally. For that purpose you can use environmental variables. Provide it in a below way:

```javascript
process.env['_sp_request_headers'] = JSON.stringify({
'X-FORMS_BASED_AUTH_ACCEPTED': 'f'
});
```
## Default options set by `sp-request`

```javascript
{
responseType: 'json',
resolveBodyOnly: false,
rejectUnauthorized: false,
retry: 0
}
```

## Development:

I recommend using VS Code for development. Repository already contains some settings for VS Code editor.

Before creating Pull Request you need to create an appropriate issue and reference it from PR.
Expand All @@ -128,9 +170,11 @@ Before creating Pull Request you need to create an appropriate issue and referen
3. `gulp live-dev` - setup watchers and automatically runs typescript compilation, tslint and tests when you save files

## Tests:

1. `npm test`. As a result `/reports` folder will be created with test results in junit format and code coverage. Additionally test reports will be available in a console window.

## Integration testing:

1. Rename file `/test/integration/config.sample.ts` to `config.ts`.
2. Update information in `config.ts` with appropriate values (urls, credentials, environment).
3. Run `gulp test-int`.
3. Run `gulp test-int`.
45 changes: 45 additions & 0 deletions UpgradeTo3x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Migration from `sp-request` 2.x to 3.x

`sp-request` 3.x uses [got](https://github.com/sindresorhus/got/) instead of [request-promise](https://github.com/request/request-promise/) (deprecated).

In most cases it has exactly the same API. However, some configuration options are different.

Please refer to the table below to see what have changed:

| old options property (request-promise) | new property (got) |
|--------------|--------------|
| `simple: true` | `throwHttpErrors: true` |
| `strictSSL: false` | `rejectUnauthorized: false`|
| `json: true` | `responseType: 'json'` |
|`json: false`| `responseType: 'text'` or `responseType: 'buffer'` |
|`resolveWithFullResponse: true`|`resolveBodyOnly: false`|
|`encoding: null` (to get file content)|`responseType: 'buffer'`|

If you use any of the properties in the left column, you should use right equivalent instead in your code.
In some situations, you might receive different error object, if you depend on error handling\checking in your code. To make sure everything works without problems, you should check error branch logic as well.

For example, your old 2.x code for `sp-request` might look like:

```javascript
spr.post("https://sharepoint.url", {
json: true,
body: {
"Title": "hello world"
},
resolveWithFullResponse: false,
simple: false
});
```

To have it in 3.x version, you should change as below:

```javascript
spr.post("https://sharepoint.url", {
responseType: 'json'
body: {
"Title": "hello world"
},
resolveBodyOnly: true,
throwHttpErrors: false
});
```
Loading