Skip to content

Commit

Permalink
Merge pull request #36 from xapijs/issue-34
Browse files Browse the repository at this point in the history
fixed #34 - stringify actor query param
  • Loading branch information
CookieCookson authored Aug 5, 2020
2 parents e3b06ff + 215f024 commit 90ac626
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/XAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ describe("statement resource", () => {
});
});

test("can query for statements using the actor property", () => {
return xapi.getStatements({
agent: testAgent
}).then((result) => {
return expect(result.statements).toBeTruthy();
});
});

test("can query a single statement using the limit property", () => {
return xapi.getStatements({
limit: 1
Expand Down
46 changes: 27 additions & 19 deletions src/XAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Resource, GetStatementQuery, GetVoidedStatementQuery, GetStatementsQuery, StatementsResponse } from "./interfaces/XAPI";
import { Resource, GetStatementQuery, GetVoidedStatementQuery, GetStatementsQuery, StatementsResponse, RequestParams } from "./interfaces/XAPI";
import { Statement, Actor, Agent, Person } from "./interfaces/Statement";
import { About } from "./interfaces/About/About";
import { AttachmentUsages, Resources, Verbs } from "./constants";
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class XAPI {
// Agents Resource
public getAgent(agent: Agent): Promise<Person> {
return this.request(Resources.AGENTS, {
agent: JSON.stringify(agent)
agent: agent
});
}

Expand Down Expand Up @@ -93,7 +93,7 @@ export default class XAPI {
// State Resource
public createState(agent: Agent, activityId: string, stateId: string, state: {[key: string]: any}, registration?: string): Promise<void> {
return this.request(Resources.STATE, {
agent: JSON.stringify(agent),
agent: agent,
activityId: activityId,
stateId: stateId,
...(registration ? {
Expand All @@ -107,7 +107,7 @@ export default class XAPI {

public setState(agent: Agent, activityId: string, stateId: string, state: {[key: string]: any}, registration?: string): Promise<void> {
return this.request(Resources.STATE, {
agent: JSON.stringify(agent),
agent: agent,
activityId: activityId,
stateId: stateId,
...(registration ? {
Expand All @@ -121,7 +121,7 @@ export default class XAPI {

public getStates(agent: Agent, activityId: string, registration?: string): Promise<string[]> {
return this.request(Resources.STATE, {
agent: JSON.stringify(agent),
agent: agent,
activityId: activityId,
...(registration ? {
registration
Expand All @@ -131,7 +131,7 @@ export default class XAPI {

public getState(agent: Agent, activityId: string, stateId: string, registration?: string): Promise<{[key: string]: any}> {
return this.request(Resources.STATE, {
agent: JSON.stringify(agent),
agent: agent,
activityId: activityId,
stateId: stateId,
...(registration ? {
Expand All @@ -142,7 +142,7 @@ export default class XAPI {

public deleteState(agent: Agent, activityId: string, stateId: string, registration?: string): Promise<void> {
return this.request(Resources.STATE, {
agent: JSON.stringify(agent),
agent: agent,
activityId: activityId,
stateId: stateId,
...(registration ? {
Expand All @@ -155,7 +155,7 @@ export default class XAPI {

public deleteStates(agent: Agent, activityId: string, registration?: string): Promise<void> {
return this.request(Resources.STATE, {
agent: JSON.stringify(agent),
agent: agent,
activityId: activityId,
...(registration ? {
registration
Expand Down Expand Up @@ -211,7 +211,7 @@ export default class XAPI {
// Agent Profile Resource
public createAgentProfile(agent: Agent, profileId: string, profile: {[key: string]: any}): Promise<void> {
return this.request(Resources.AGENT_PROFILE, {
agent: JSON.stringify(agent),
agent: agent,
profileId: profileId
}, {
method: "POST",
Expand All @@ -221,7 +221,7 @@ export default class XAPI {

public setAgentProfile(agent: Agent, profileId: string, profile: {[key: string]: any}): Promise<void> {
return this.request(Resources.AGENT_PROFILE, {
agent: JSON.stringify(agent),
agent: agent,
profileId: profileId
}, {
method: "PUT",
Expand All @@ -231,29 +231,28 @@ export default class XAPI {

public getAgentProfiles(agent: Agent): Promise<string[]> {
return this.request(Resources.AGENT_PROFILE, {
agent: JSON.stringify(agent)
agent: agent
});
}

public getAgentProfile(agent: Agent, profileId: string): Promise<{[key: string]: any}> {
return this.request(Resources.AGENT_PROFILE, {
agent: JSON.stringify(agent),
agent: agent,
profileId: profileId
});
}

public deleteAgentProfile(agent: Agent, profileId: string): Promise<void> {
return this.request(Resources.AGENT_PROFILE, {
agent: JSON.stringify(agent),
agent: agent,
profileId: profileId
}, {
method: "DELETE"
});
}

private request(resource: Resource, params: {[key: string]: any} = {}, init?: RequestInit | undefined): Promise<any> {
const queryString: string = Object.keys(params).map(key => key + "=" + encodeURIComponent(params[key])).join("&");
const url: RequestInfo = `${this.endpoint}${resource}${queryString ? "?" + queryString : ""}`;
private request(resource: Resource, params: RequestParams = {}, init?: RequestInit | undefined): Promise<any> {
const url = this.generateURL(resource, params);
return fetch(url, {
headers: this.headers,
...init
Expand All @@ -273,11 +272,10 @@ export default class XAPI {
});
}

private requestXMLHTTPRequest(resource: Resource, params: {[key: string]: any} = {}, initExtras?: RequestInit | undefined): Promise<any> {
private requestXMLHTTPRequest(resource: Resource, params: RequestParams = {}, initExtras?: RequestInit | undefined): Promise<any> {
return new Promise((resolve, reject) => {
const xmlRequest = new XMLHttpRequest();
const queryString: string = Object.keys(params).map(key => key + "=" + encodeURIComponent(params[key])).join("&");
const url: RequestInfo = `${this.endpoint}${resource}${queryString ? "?" + queryString : ""}`;
const url = this.generateURL(resource, params);
xmlRequest.open(initExtras?.method || "GET", url, true);
const headers = {
...this.headers,
Expand Down Expand Up @@ -305,4 +303,14 @@ export default class XAPI {
xmlRequest.send(initExtras?.body);
});
}

private generateURL(resource: Resource, params: {[key: string]: any}): string {
const queryString = Object.keys(params).map(key => {
let val = key === "agent" ? JSON.stringify(params[key]) : params[key];
val = encodeURIComponent(val);
return `${key}=${val}`;
}).join("&");
const url: RequestInfo = `${this.endpoint}${resource}${queryString ? "?" + queryString : ""}`;
return url;
}
}
6 changes: 6 additions & 0 deletions src/interfaces/XAPI/RequestParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Agent } from "../Statement";

export interface RequestParams {
[key: string]: any;
agent?: Agent;
}
3 changes: 2 additions & 1 deletion src/interfaces/XAPI/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./Resource";
export * from "./GetStatementQuery";
export * from "./GetStatementsQuery";
export * from "./RequestParams";
export * from "./Resource";
export * from "./StatementsResponse";

0 comments on commit 90ac626

Please sign in to comment.