Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("should stringify Extended JSON correctly", () => {
expect(
EJSON.stringify({
test: 42
}, { relaxed: false })
).toEqual('{"test":{"$numberInt":"42"}}');
});
it("should deserialize Extended JSON correctly", () => {
private getStreamServiceFunctionRequest(
name: string,
args: any[]
): StitchAuthRequest {
const body = { name };
if (this.serviceName !== undefined) {
body[this.serviceField] = this.serviceName;
}
body[this.argumentsField] = args;
const reqBuilder = new StitchAuthRequest.Builder();
reqBuilder
.withMethod(Method.GET)
.withPath(this.serviceRoutes.functionCallRoute +
`?stitch_request=${encodeURIComponent(base64Encode(EJSON.stringify(body)))}`);
return reqBuilder.build();
}
async execute(query) {
try {
const payload = EJSON.stringify({
query,
database: { server: this.server, name: this.databaseName }
});
const res = await fetch(process.env.MONGO_HTTP_SERVER, {
method: "post",
body: payload,
headers: { "Content-Type": "application/json" }
});
if (res.status >= 400) {
throw new Error("Bad response from server");
}
const data = await res.text();
return EJSON.parse(data).result;
public build(): StitchDocRequest {
if (this.document === undefined || !(this.document instanceof Object)) {
throw new Error("document must be set");
}
if (this.headers === undefined) {
this.withHeaders({});
}
this.headers![Headers.CONTENT_TYPE] = ContentTypes.APPLICATION_JSON;
this.withBody(EJSON.stringify(this.document, { relaxed: false }));
return new StitchDocRequest(super.build(), this.document);
}
}
protected _create<u>(data: U, type: TypeCtor<u>): Promise<u> {
const reqBuilder = new StitchAuthRequest.Builder();
reqBuilder
.withMethod(Method.POST)
.withPath(this.url)
.withBody(EJSON.stringify(serialize(data), { relaxed: false }));
return this.adminAuth
.doAuthenticatedRequest(reqBuilder.build())
.then(response =>
deserialize(EJSON.parse(response.body!), type)
);
}
</u></u></u>
protected _update(data: T, putOrPatch: Method.PATCH | Method.PUT): Promise {
const reqBuilder = new StitchAuthRequest.Builder();
reqBuilder
.withMethod(putOrPatch)
.withPath(this.url)
.withBody(EJSON.stringify(serialize(data), { relaxed: false }));
return this.adminAuth
.doAuthenticatedRequest(reqBuilder.build())
.then((response) => {
return;
});
}
public build(): StitchAuthDocRequest {
if (this.document === undefined || !(this.document instanceof Object)) {
throw new Error("document must be set: " + this.document);
}
if (this.headers === undefined) {
this.withHeaders({});
}
this.headers![Headers.CONTENT_TYPE] = ContentTypes.APPLICATION_JSON;
this.withBody(EJSON.stringify(this.document, { relaxed: false }));
return new StitchAuthDocRequest(super.build(), this.document);
}
}