Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { EJSON } = require('bson')
const text = '{ "int32": { "$numberInt": "10" } }'
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
console.log(EJSON.parse(text, { relaxed: false }))
// prints { int32: 10 }
console.log(EJSON.parse(text))
.then(response => {
expect(response.statusCode).toBe(200);
const expected = {
a: 42,
hello: "world"
};
expect(expected).toEqual(EJSON.parse(response.body!, { relaxed: true }));
// Error responses should be handled
when(transportMock.roundTrip(anything())).thenResolve({
headers: {},
statusCode: 500
});
return stitchRequestClient.doRequest(builder.build());
})
.catch((error: StitchServiceError) => {
.then(response => {
expect(response.statusCode).toEqual(200);
const expected = {
a: 42,
hello: "world"
};
expect(EJSON.parse(response.body!, { relaxed: true })).toEqual(expected);
// Error responses should be handled
when(transportMock.roundTrip(anything())).thenResolve({
headers: {},
statusCode: 500
});
return stitchRequestClient.doRequest(builder.build());
})
.catch((error: StitchServiceError) => {
/*
* parse the error as json
* if it is not valid json, parse the body as seen in
* StitchError#handleRequestError
*/
const errorDoc = EJSON.parse(decodedData, { strict: false });
errorMsg = errorDoc[ErrorFields.Error];
errorCode = stitchServiceErrorCodeFromApi(errorDoc[ErrorFields.ErrorCode]);
} catch (error) {
errorMsg = decodedData;
errorCode = StitchServiceErrorCode.Unknown;
}
this.error = new StitchServiceError(errorMsg, errorCode);
break;
case Event.MESSAGE_EVENT:
this.data = EJSON.parse(decodedData, { strict: false });
if (decoder) {
this.data = decoder.decode(this.data);
}
break;
}
}
}
const response = await httpClient.execute(goodRequest);
if (i !== retryAttempts && response.statusCode !== 200) {
await sleep(5000);
continue;
}
expect("200 OK").toEqual(response.status);
expect(200).toEqual(response.statusCode);
expect(response.contentLength).toBeGreaterThanOrEqual(300);
expect(response.contentLength).toBeLessThanOrEqual(500);
expect(response.body).toBeDefined();
expect(response.headers['Content-Type'].length).toEqual(1);
expect(response.headers['Content-Type'][0]).toEqual('application/json');
const dataDoc = EJSON.parse(String(response.body!!), { relaxed: true });
expect(body).toEqual(dataDoc.data);
const headersDoc = dataDoc.headers;
expect("value1,value2").toEqual(headersDoc.Myheader);
expect("bob=barker").toEqual(headersDoc.Cookie);
}
});
});
readFile(path: string): any {
const fileExtension = extname(path);
if (fileExtension === '.json') {
const content = readFileSync(path, 'utf-8');
return EJSON.parse(content, {
relaxed: true,
});
}
return require(path);
}
}
.then(response =>
deserialize(EJSON.parse(response.body!), type)
);
return this.adminAuth.doAuthenticatedRequest(req).then(response => {
checkEmpty(response);
return deserialize(EJSON.parse(response.body!), App);
});
}
.then(response =>
EJSON.parse(response.body!).map(val => deserialize(val, type))
);
.then(resp => {
this.appMetadata = AppMetadata.fromJSON(EJSON.parse(resp.body));
return this.appMetadata;
});
}