Skip to content

Commit

Permalink
Merge pull request #27 from onready/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Juan Cernadas authored Jul 3, 2020
2 parents 118e52e + c904098 commit 3009fa1
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Payments Gateway | :white_check_mark: |
Suggestions | :white_check_mark: |
VTEX DO | :white_check_mark: |
CMS | :white_check_mark: |
Session Manager | :white_check_mark: |
Antifraud Provider | :x: |
Checkout | :x: |
Customer Credit | :x: |
Expand All @@ -112,5 +113,4 @@ Giftcard Provider Protocol | :x: |
License Manager | :x: |
Payment Provider Protocol | :x: |
Rates and Benefits | :x: |
Session Manager | :x: |
Subscriptions (V2) | :x: |
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onreadydesa/vtex-node-sdk",
"version": "1.9.0-beta",
"version": "1.10.0-beta",
"description": "VTEX Node SDK, built 100% with Typescript and 0 dependencies!",
"author": "Onready",
"private": false,
Expand Down Expand Up @@ -46,7 +46,7 @@
"@typescript-eslint/eslint-plugin": "3.5.0",
"@typescript-eslint/parser": "3.5.0",
"babel-jest": "26.1.0",
"eslint": "7.3.1",
"eslint": "7.4.0",
"eslint-config-airbnb-base": "14.2.0",
"eslint-config-prettier": "6.11.0",
"eslint-plugin-import": "2.22.0",
Expand Down
7 changes: 7 additions & 0 deletions src/VTEX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { VtexDo } from "./modules/vtex-do";
import { Suggestions } from "./modules/suggestions";
import { CMS } from "./modules/CMS";
import { SessionManager } from "./modules/session-manager";

export class VTEX {
private static buildErrorMessage(paramName: string): string {
Expand Down Expand Up @@ -87,6 +88,11 @@ export class VTEX {
*/
readonly cms: CMS;

/**
* Session Manager Module
*/
readonly sessionManager: SessionManager;

/**
* @param {string} store
* @param {string} appKey
Expand Down Expand Up @@ -121,5 +127,6 @@ export class VTEX {
this.vtexDo = new VtexDo(vtexHttpClient);
this.suggestions = new Suggestions(vtexHttpClient);
this.cms = new CMS(vtexHttpClient);
this.sessionManager = new SessionManager(vtexHttpClient);
}
}
20 changes: 20 additions & 0 deletions src/modules/session-manager/SessionManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { VtexHttpClient } from "../../utils/VtexHttpClient";
import { Sessions } from "./apis/sessions";
import { Segment } from "./apis/segment";

export class SessionManager {
/**
* Sessions API
*/
readonly sessions: Sessions;

/**
* Segment API
*/
readonly segment: Segment;

constructor(vtexHttpClient: VtexHttpClient) {
this.sessions = new Sessions(vtexHttpClient);
this.segment = new Segment(vtexHttpClient);
}
}
18 changes: 18 additions & 0 deletions src/modules/session-manager/apis/segment/Segment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AbstractApi } from "../../../AbstractApi";
import { VtexHttpResponse } from "../../../../utils/VtexHttpResponse";

export class Segment extends AbstractApi {
/**
* You can add certain public fields in the querystring and the system
* will attempt to fulfill it. Values such as cultureInfo and utm are
* overwriteable, just keep in mind such changes will not be reflected
* in the client's session.If you wish to change the value on the session
* (and thus be reflected on the segment without special querystrings),
* then use the PATCH request to session.
* @param {string} params Example: param1=test&param2=test2
*/
getSegment(params?: string): Promise<VtexHttpResponse> {
const path = `/api/segments?${params}`;
return this.vtexHttpClient.performRequest(path, this.HTTP_METHODS.GET);
}
}
1 change: 1 addition & 0 deletions src/modules/session-manager/apis/segment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Segment";
59 changes: 59 additions & 0 deletions src/modules/session-manager/apis/sessions/Sessions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { AbstractApi } from "../../../AbstractApi";
import { CreateNewSessionRequest } from "./requests/CreateNewSessionRequest";
import { VtexHttpResponse } from "../../../../utils/VtexHttpResponse";

export class Sessions extends AbstractApi {
private static readonly BASE_PATH: string = "/api/sessions";

/**
* The response should contain a session cookie.
* Further POST or PATCH requests will edit the existing session
* rather than creating a new one. All parameters in the body that are not within
* the public namespace will be ignored. Querystring items will automatically be
* added to the public namespace. Cookies relevant to the session manager execution
* are also recorded.
* @param {CreateNewSessionRequest} newSessionData
*/
createNewSession(
newSessionData: CreateNewSessionRequest
): Promise<VtexHttpResponse> {
return this.vtexHttpClient.performRequest(
Sessions.BASE_PATH,
this.HTTP_METHODS.POST,
newSessionData
);
}

/**
* Items are the keys of the values you wish to get.
* It follows the format namespace1.key1,namespace2.key2.
* So if you wish to recover the data sent on the previous request,
* it should be public.country,public.postalCode
* @param {string} items
*/
getSession(items: string): Promise<VtexHttpResponse> {
const path = `${Sessions.BASE_PATH}?items=${items}`;
return this.vtexHttpClient.performRequest(path, this.HTTP_METHODS.GET);
}

/**
* This works exactly the same as the POST create session, but when the request
* is sent with a vtex_session cookie, it retrieves the session first and then
* applies the changes instead of generating a new one. As with the POST method,
* only keys inside the public namespace on the body are considered, and querystrings
* are automatically added to the public namespace.
* @param {any} headers
* @param {CreateNewSessionRequest} newSessionData
*/
editSession(
headers: any,
newSessionData: CreateNewSessionRequest
): Promise<VtexHttpResponse> {
return this.vtexHttpClient.performRequest(
Sessions.BASE_PATH,
this.HTTP_METHODS.PATCH,
newSessionData,
headers
);
}
}
1 change: 1 addition & 0 deletions src/modules/session-manager/apis/sessions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Sessions";
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface Country {
value?: string;
}

interface PostalCode {
value?: string;
}

interface Public {
country?: Country;
postalCode?: PostalCode;
}

export interface CreateNewSessionRequest {
public?: Public;
}
1 change: 1 addition & 0 deletions src/modules/session-manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./SessionManager";
3 changes: 2 additions & 1 deletion src/utils/VtexHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export class VtexHttpClient {
}
const vtexHttpResponse: VtexHttpResponse = new VtexHttpResponse(
response.statusCode,
jsonResponse
jsonResponse,
response.headers
);
if (response.statusCode < 400) {
resolve(vtexHttpResponse);
Expand Down
21 changes: 18 additions & 3 deletions src/utils/VtexHttpResponse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { IncomingHttpHeaders } from "http";

export class VtexHttpResponse<T = any> {
status: number;
/**
* Response status
*/
readonly status: number;

/**
* Response body
*/
readonly body: T;

body: T;
/**
* Response headers
*/
readonly headers: IncomingHttpHeaders;

/**
* @param {number} status
* @param {T} body
* @param {IncomingHttpHeaders} headers
*/
constructor(status: number, body: T) {
constructor(status: number, body: T, headers: IncomingHttpHeaders) {
this.status = status;
this.body = body;
this.headers = headers;
}
}
4 changes: 4 additions & 0 deletions test/VTEX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ describe("VTEX tests", () => {

expect(instance.cms).not.toBe(null);
expect(instance.cms.changeUriSchema).not.toBe(null);

expect(instance.sessionManager).not.toBe(null);
expect(instance.sessionManager.sessions).not.toBe(null);
expect(instance.sessionManager.segment).not.toBe(null);
done();
}));

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2366,10 +2366,10 @@ eslint-visitor-keys@^1.1.0, eslint-visitor-keys@^1.2.0:
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.2.0.tgz#74415ac884874495f78ec2a97349525344c981fa"
integrity sha512-WFb4ihckKil6hu3Dp798xdzSfddwKKU3+nGniKF6HfeW6OLd2OUDEPP7TcHtB5+QXOKg2s6B2DaMPE1Nn/kxKQ==

eslint@7.3.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.3.1.tgz#76392bd7e44468d046149ba128d1566c59acbe19"
integrity sha512-cQC/xj9bhWUcyi/RuMbRtC3I0eW8MH0jhRELSvpKYkWep3C6YZ2OkvcvJVUeO6gcunABmzptbXBuDoXsjHmfTA==
eslint@7.4.0:
version "7.4.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.4.0.tgz#4e35a2697e6c1972f9d6ef2b690ad319f80f206f"
integrity sha512-gU+lxhlPHu45H3JkEGgYhWhkR9wLHHEXC9FbWFnTlEkbKyZKWgWRLgf61E8zWmBuI6g5xKBph9ltg3NtZMVF8g==
dependencies:
"@babel/code-frame" "^7.0.0"
ajv "^6.10.0"
Expand Down

0 comments on commit 3009fa1

Please sign in to comment.