-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from onready/develop
Develop
- Loading branch information
Showing
14 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¶m2=test2 | ||
*/ | ||
getSegment(params?: string): Promise<VtexHttpResponse> { | ||
const path = `/api/segments?${params}`; | ||
return this.vtexHttpClient.performRequest(path, this.HTTP_METHODS.GET); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Segment"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Sessions"; |
16 changes: 16 additions & 0 deletions
16
src/modules/session-manager/apis/sessions/requests/CreateNewSessionRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./SessionManager"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters