Skip to content

Commit 9f7a5d1

Browse files
committed
feat: add RESTlet support
- Adds the ability to use a RESTlet URL in place of the REST api by extending the `client.request()` method
1 parent 5a63e39 commit 9f7a5d1

File tree

6 files changed

+81
-16
lines changed

6 files changed

+81
-16
lines changed

restlet-demo.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @NAPIVersion 2.0
3+
* @NScriptType Restlet
4+
*/
5+
6+
/*
7+
This file was generated via the
8+
headintheclouds [typings-suitescript-2.0[(https://github.com/headintheclouddev/typings-suitescript-2.0) project.
9+
10+
It can be deployed as a restlet and will return a successful response to any GET request.
11+
*/
12+
13+
var __importDefault = (this && this.__importDefault) || function (mod) {
14+
return (mod && mod.__esModule) ? mod : { "default": mod };
15+
};
16+
define(["N/log"], function (log_1) {
17+
var exports = {};
18+
Object.defineProperty(exports, "__esModule", { value: true });
19+
exports.get = void 0;
20+
log_1 = __importDefault(log_1);
21+
var get = function (requestParams) {
22+
log_1.default.debug({
23+
title: 'GET Request',
24+
details: requestParams,
25+
});
26+
return {
27+
success: true,
28+
};
29+
};
30+
exports.get = get;
31+
return exports;
32+
});

src/client.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,20 @@ export default class NetsuiteApiClient {
7070
* @returns
7171
*/
7272
public async request(opts: NetsuiteRequestOptions) {
73-
const { path = "*", method = "GET", body = "", heads = {} } = opts;
73+
const { path = "*", method = "GET", body = "", heads = {}, restletUrl } = opts;
7474
const cleanPath = removeLeadingSlash(path);
7575
// Set up the Request URI
76-
let uri;
77-
if (this.base_url) {
78-
uri = `${this.base_url}/services/rest/${cleanPath}`
79-
} else {
80-
// as suggested by dylbarne in #15: sanitize url to enhance overall usability
81-
uri = `https://${this.realm
76+
77+
// as suggested by dylbarne in #15: sanitize url to enhance overall usability
78+
let uri = `https://${this.realm
8279
.toLowerCase()
8380
.replace("_", "-")}.suitetalk.api.netsuite.com/services/rest/${cleanPath}`;
81+
if (this.base_url) {
82+
uri = `${this.base_url}/services/rest/${cleanPath}`
83+
}
84+
// if restletUrl is provided, use it instead of the default uri
85+
if (restletUrl) {
86+
uri = restletUrl;
8487
}
8588

8689
const options = {

src/types.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type {Buffer} from "node:buffer";
2+
import type {Readable} from "node:stream";
3+
14
export type NetsuiteOptions = {
25
consumer_key: string;
36
consumer_secret_key: string;
@@ -7,13 +10,25 @@ export type NetsuiteOptions = {
710
base_url?: string;
811
};
912

10-
export type NetsuiteRequestOptions = {
11-
path?: string;
12-
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options';
13-
body?: any;
14-
heads?: any;
13+
type BaseRequestOptions = {
14+
/**
15+
* The HTTP method to use
16+
*/
17+
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options';
18+
/**
19+
* The body of the request
20+
*/
21+
body?: string;
22+
/**
23+
* Additional headers to send with the request
24+
*/
25+
heads?: any;
1526
};
1627

28+
export type NetsuiteRequestOptions =
29+
| (BaseRequestOptions & { path?: string; restletUrl?: never })
30+
| (BaseRequestOptions & { path?: never; restletUrl?: string });
31+
1732
export type NetsuiteResponse = {
1833
statusCode: number;
1934
headers: NodeJS.Dict<string | string[]>;

test/basic.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "dotenv/config";
2-
import NetsuiteApiClient from "../src/client";
2+
import NetsuiteApiClient from "../src/client.js";
33
import { beforeAll, describe, expect, it } from "vitest";
44

55
let client: NetsuiteApiClient;

test/query.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "dotenv/config";
2-
import NetsuiteApiClient from "../src/client";
2+
import NetsuiteApiClient from "../src/client.js";
33
import { beforeAll, describe, expect, it } from "vitest";
44

55
let client: NetsuiteApiClient;

test/request.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "dotenv/config";
2-
import NetsuiteApiClient from "../src/client";
2+
import NetsuiteApiClient from "../src/client.js";
33
import { describe, expect, test, beforeAll, afterAll, it } from "vitest";
44

55
describe("Test request method", () => {
@@ -13,7 +13,8 @@ describe("Test request method", () => {
1313
process.env.token == undefined ||
1414
process.env.token_secret == undefined ||
1515
process.env.realm == undefined ||
16-
process.env.base_url == undefined
16+
process.env.base_url == undefined ||
17+
process.env.restlet_url == undefined
1718
) {
1819
throw new Error("Please create a `.env` file based on `.env.sample`");
1920
}
@@ -45,6 +46,18 @@ describe("Test request method", () => {
4546
expect(response.statusCode).toEqual(204);
4647
});
4748

49+
// it('should make a RESTlet request', async () => {
50+
// expect.assertions(1);
51+
// const response = await client.request({
52+
// method: 'GET',
53+
// restletUrl: process.env.restlet_url,
54+
// heads: {
55+
// 'Content-Type': 'application/json'
56+
// }
57+
// })
58+
// expect(response.statusCode).toEqual(200);
59+
// })
60+
4861
it("should make GET request - GET Customers", async () => {
4962
expect.assertions(4);
5063
const response = await client.request({
@@ -131,6 +144,8 @@ describe("Test request method", () => {
131144
path: "record/v1/bad-path",
132145
})
133146
).rejects.toThrowError("Record type 'bad-path' does not exist");
147+
}, {
148+
timeout: 10000,
134149
});
135150

136151
it("should reject with meaningful error if bad object", async () => {

0 commit comments

Comments
 (0)