Skip to content

Commit

Permalink
ContentId can be used as an URI reference inside the Batch Request Ch…
Browse files Browse the repository at this point in the history
…angeset
  • Loading branch information
AleksandrRogov committed Jan 5, 2024
1 parent 3c6bff6 commit 618280f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/dynamics-web-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ export interface BatchRequest extends BaseRequest {

export interface Request extends BaseRequest {
/**A name of the Entity Collection or Entity Logical name. */
collection: string;
collection?: string;
}

export interface CRUDRequest extends Request {
Expand All @@ -1221,6 +1221,8 @@ export interface CountRequest extends Request {
}

export interface CountAllRequest extends CountRequest {
/**A name of the Entity Collection or Entity Logical name. */
collection: string;
/**An Array (of strings) representing the $select OData System Query Option to control which attributes will be returned. */
select?: string[];
}
Expand Down Expand Up @@ -1332,6 +1334,8 @@ export interface DeleteRequest extends CRUDRequest {
}

export interface RetrieveRequest extends CRUDRequest {
/**A name of the Entity Collection or Entity Logical name. */
collection: string;
/**An array of Expand Objects(described below the table) representing the $expand OData System Query Option value to control which related records are also returned. */
expand?: Expand[];
/**Sets If-Match header value that enables to use conditional retrieval or optimistic concurrency in applicable requests.*/
Expand All @@ -1357,6 +1361,8 @@ export interface RetrieveRequest extends CRUDRequest {
}

export interface RetrieveMultipleRequest extends Request {
/**A name of the Entity Collection or Entity Logical name. */
collection: string;
/**Use the $apply to aggregate and group your data dynamically */
apply?: string;
/**An array of Expand Objects(described below the table) representing the $expand OData System Query Option value to control which related records are also returned. */
Expand Down
16 changes: 8 additions & 8 deletions src/utils/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ export class RequestUtility {
request.path = request.path || "";
request.functionName = request.functionName || "";
if (!request.url) {
if (!request._isUnboundRequest && !request.collection) {
if (!request._isUnboundRequest && !request.contentId && !request.collection) {
ErrorHelper.parameterCheck(request.collection, `DynamicsWebApi.${request.functionName}`, "request.collection");
}
if (request.collection != null) {
ErrorHelper.stringParameterCheck(request.collection, `DynamicsWebApi.${request.functionName}`, "request.collection");
request.path = request.collection;

if (request.contentId) {
ErrorHelper.stringParameterCheck(request.contentId, `DynamicsWebApi.${request.functionName}`, "request.contentId");
if (request.contentId.startsWith("$")) {
request.path = `${request.contentId}/${request.path}`;
}
}

//add alternate key feature
if (request.key) {
request.key = ErrorHelper.keyParameterCheck(request.key, `DynamicsWebApi.${request.functionName}`, "request.key");
request.path += `(${request.key})`;
}
}

if (request.contentId) {
ErrorHelper.stringParameterCheck(request.contentId, `DynamicsWebApi.${request.functionName}`, "request.contentId");
if (request.contentId.startsWith("$")) {
request.path = request.path ? `${request.contentId}/${request.path}` : request.contentId;
}
}

if (request._additionalUrl) {
if (request.path) {
request.path += "/";
Expand Down
56 changes: 56 additions & 0 deletions tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,62 @@ describe("dynamicsWebApi.executeBatch -", () => {
expect(scope.isDone()).to.be.true;
});
});

describe("create / create with Content-ID / No Collection", function () {
let scope;
const rBody = mocks.data.batchCreateContentIDNoCollection;
const rBodys = rBody.split("\n");
let checkBody = "";
for (var i = 0; i < rBodys.length; i++) {
checkBody += rBodys[i];
}
before(function () {
const response = mocks.responses.batchUpdateDelete;
scope = nock(mocks.webApiUrl)
.filteringRequestBody(function (body) {
body = body.replace(/dwa_batch_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, "dwa_batch_XXX");
body = body.replace(/changeset_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, "changeset_XXX");
var bodys = body.split("\n");

var resultBody = "";
for (var i = 0; i < bodys.length; i++) {
resultBody += bodys[i];
}
return resultBody;
})
.post("/$batch", checkBody)
.reply(response.status, response.responseText, response.responseHeaders);
});

after(function () {
nock.cleanAll();
});

it("returns a correct response", async () => {
dynamicsWebApiTest.startBatch();

dynamicsWebApiTest.create({ collection: "records", data: { firstname: "Test", lastname: "Batch!" }, contentId: "1" });
dynamicsWebApiTest.create({ data: { firstname: "Test1", lastname: "Batch!" }, contentId: "$1" });

try{
const object = await dynamicsWebApiTest
.executeBatch();

expect(object.length).to.be.eq(2);

expect(object[0]).to.be.eq(mocks.data.testEntityId);
expect(object[1]).to.be.undefined;
}
catch(error) {
console.error(error);
throw error;
}
});

it("all requests have been made", () => {
expect(scope.isDone()).to.be.true;
});
});
});

describe("dynamicsWebApi: custom headers - ", () => {
Expand Down
29 changes: 29 additions & 0 deletions tests/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,35 @@ const dataStubs = {
"--changeset_XXX--\n" +
"\n" +
"--dwa_batch_XXX--",
batchCreateContentIDNoCollection:
"--dwa_batch_XXX\n" +
"Content-Type: multipart/mixed;boundary=changeset_XXX\n" +
"\n" +
"--changeset_XXX\n" +
"Content-Type: application/http\n" +
"Content-Transfer-Encoding: binary\n" +
"Content-ID: 1\n" +
"\n" +
"POST " +
webApiUrl +
"records HTTP/1.1\n" +
"Content-Type: application/json\n" +
"\n" +
'{"firstname":"Test","lastname":"Batch!"}\n' +
"\n" +
"--changeset_XXX\n" +
"Content-Type: application/http\n" +
"Content-Transfer-Encoding: binary\n" +
"Content-ID: 100001\n" +
"\n" +
"POST $1 HTTP/1.1\n" +
"Content-Type: application/json\n" +
"\n" +
'{"firstname":"Test1","lastname":"Batch!"}\n' +
"\n" +
"--changeset_XXX--\n" +
"\n" +
"--dwa_batch_XXX--",
batchCreateContentIDURLReplace:
"--dwa_batch_XXX\n" +
"Content-Type: multipart/mixed;boundary=changeset_XXX\n" +
Expand Down

0 comments on commit 618280f

Please sign in to comment.