Coder Social home page Coder Social logo

ehmad11 / netsuite-rest Goto Github PK

View Code? Open in Web Editor NEW
46.0 4.0 18.0 947 KB

Make requests to Oracle NetSuite REST Web Services.

License: MIT License

JavaScript 100.00%
nodejs npm-package netsuite netsuite-rest package node node-module rest rest-client oauth tba token js javascript

netsuite-rest's Introduction

NetSuite - SuiteTalk REST Web Services

NPM

Node.js CI npm version downloads Coverage Status FOSSA Status

Make requests to SuiteTalk REST Web Services

Installation

npm i netsuite-rest

ESM or CommonJS?

This package is still compatible with CommonJS. But some dependencies, like got are now available only in ESM. This package will not be modified to support latest got versions to stay compatible with CommonJS. Instead you can use the native ESM module netsuite-api-client, which is a fork of the current package.

Quick Start

To set up TBA in Netsuite, see the help topic Getting Started with Token-based Authentication.

var NsApiWrapper = require('netsuite-rest');
NsApi = new NsApiWrapper({
	consumer_key: 'consumer_key',
	consumer_secret_key: 'consumer_secret_key',
	token: 'token',
	token_secret: 'token_secret',
	realm: 'realm'
	//,base_url: 'base_url' // optional
});

Sample Requests

All requests are signed.

Test Request

NsApi.request({
    path: '*',
    method: "OPTIONS"
})
.then(response => console.log(response))
.catch((err) => console.log(err));

GET Request:

NsApi.request({
    path: 'record/v1/customer/'
})
.then(response => response.data)
.then(data => console.log(data.links))
.catch((err) => console.log(err));

SuiteQL

NOTE: If you are interested only in the SuiteQL, check SuiteQL package or view source code. SuiteQL class extends this class and can return promise or stream for large number of rows.

SuiteQL is a subservice of the query service. Following is an example to execute SuiteQL queries:

NsApi.request({
    path: 'query/v1/suiteql?limit=5',
    method: "POST",
    body: `{
		"q":
		"SELECT
			id, companyName, email, dateCreated
         FROM customer WHERE
            dateCreated >= '01/01/2019'
           	AND dateCreated < '01/01/2020'"
	}`
})
.then(response => console.log(response))
.catch((err) => console.log(err));

Response

Requests are returned with promise support (.then(...)). HTTP response codes other than 2xx will cause the promise to be rejected.

Metadata

NsApi.request({path: 'record/v1/metadata-catalog/'})

Record is the name of the service we are trying to access, v1 is the service version, and metadata-catalog is the sub-resource, that is, the record metadata. The response informs you through HATEOAS links about the possible mediaType flavor in which the response can be obtained.

HATEOAS

You can navigate to the referenced resources without deeper knowledge of the system. A typical response contains "links" sections for each resource, which can be a sub-resource of a parent resource or any other referenced resource. You can use links to work with those resources.

More Resources

SuiteTalk REST Web Services

Overview and Setup - Official Documentation

Netsuite Rest API Browser

REST API Browser provides a visual overview of the structure and capabilities of the REST web services Record API. The data presented in the REST API Browser is based on OpenAPI 3.0 metadata.

netsuite-rest's People

Contributors

dependabot[bot] avatar ehmad11 avatar jordanbrandley avatar julbrs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

netsuite-rest's Issues

HTTPError: Response code 400 (Bad Request)

Hi @ehmad11. Was wondering if I'm doing something weird/wrong.

Tried
NsApi.request({ path: '*', method: "OPTIONS" }) .then(response => console.log(response))
and that seemed to work. It spit out a big response but don't think there was a body. Not sure if that's expected.

When I try
NsApi.request({ path: 'record/v1/customer/' }) .then(response => response.data) .then(data => console.log(data.links)) .catch((err) => console.log(err));
I get HTTPError: Response code 400 (Bad Request)

Am I doing something wrong? It should return a list of customer records right?

How to pass limit and offset as parameters?

this works
return getNetSuite().request({ path: 'record/v1/salesOrder?limit=999' })
while this does not
const limit = 500; return getNetSuite().request({ path: 'record/v1/salesOrder?limit=' + limit })

some of the response is gzip-ed

In the latest version of Netsuite API, some of the (error) response becomes gzip-ed

"response": {
"statusCode": 400,
"body":"��\b�������]�AO�0\f���\n+����i�ހ�\f\n���R�DJ�a�\u000bմ�N:��Nzϟ�|R2�P�ʋ������η:Ѿz�$ɥ��}��m���׌����!��F7j�$H\n���\fN�R\�+��vS�k�Z$J�C�!������.X�$�}�\b�9d���,�r�ȡ�;pi��x.Q�9���a�C�"��g������#����&���X�a�C�y���k���南�μw�<�u;_}��LɈ+���",
"headers": {
"...
"content-encoding": "gzip",
"content-type": "application/vnd.oracle.resource+json; type=error; charset=UTF-8",
....

However, I couldn't include "gzip": true as a param in request function and allow it to be used in "requestPromise"

request(opts) {
const {
path = '*',
method = 'GET',
body = ''
} = opts;

    ...
    const options = {
        uri: uri,
        method,
        resolveWithFulLResponse: true,
        transform: (body, response) => {
            let data = {}
            if (body)
                data = JSON.parse(body)
            return {
                statusCode: response.statusCode,
                'headers': response.headers,
                'data': data
            };
        }
    };
    options.headers = this.getAuthorizationHeader(options);
    if (body) {
        options.body = body;
        options.headers.prefer = "transient";
    }
    return requestPromise(options);
}

problem with realm and URL

My realm needs to look something like this: 1234567_SB1.

If I set that as the realm, it tries to make requests to this url: https://1234567_sb1.suitetalk.api.netsuite.com/

but the URL needs to be 1234567-sb1.suitetalk.api.netsuite.com

The URL needs to have a hyphen in it while the realm needs to have an underscore.

I think there should be an option to specify the url and the realm separately if you want to.

I'm thinking maybe if there were something like a base_url option and then if it isn't specified, continue to just assume it is safe to use the realm in the url but if it is specified, use the base_url.

OAuth 2.0 accessToken

Hey, thanks for putting the library together it looks great!

I am building an application that uses OAuth 2.0 code grant flow which returns an accessToken for the user.

Is it possible to use this library in such a way that we just use the Authorization bearer ${token} instead of needing all the other config?

Thanks in advance!

Expose more info when there is an error

Hi,

When an error is occuring a NS call, I am ending with a very basic error generated by got. (like HttpError 400).

I think it's linked to this! sindresorhus/got#1126

I would like to have a bit more information like what I can see in Postman:

{
    "type": "https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
    "title": "Bad Request",
    "status": 400,
    "o:errorDetails": [
        {
            "detail": "Error while accessing a resource. Please enter value(s) for: First Name, Last Name.",
            "o:errorCode": "USER_ERROR"
        }
    ]
}

suggestion: should further sanitize url

along the same lines as #1
for instance, Release Preview accounts will have suffix "_RP" and demo accounts will have prefix "TSTDRV"

when initially setting up test request spent quite a while troubleshooting the origin of an "invalid login attempt" error, and was ultimately due to NetSuite accountId portion in Url needing to be lowercase.
I was originally comparing against Postman to confirm I had valid credentials, and was working as expected there so was puzzled, but appears Postman automatically does this lowercasing to some degree (if you test with uppercase accountId Url host and inspect outgoing request via tool like Fiddler)

suggestion for ease of use would be to further sanitize default Url used (like are doing already for Sandbox accounts)

could update code to use https://${this.realm.toLowerCase().replace("_",'-')}.suitetalk.api.netsuite.com/services/rest/${path}

I realize this can be overcome with base_url param, but I believe this change will enhance overall usability and hopefully prevent someone running into troubleshooting the issue I saw in the future 😄

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.