Skip to content

Commit ae44d23

Browse files
committed
changes
1 parent 4063a50 commit ae44d23

File tree

2 files changed

+91
-128
lines changed

2 files changed

+91
-128
lines changed

packages/compass-user-data/src/user-data.spec.ts

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,12 @@ describe('AtlasUserData', function () {
366366
| 'FavoriteQueries'
367367
| 'SavedPipelines' = 'FavoriteQueries'
368368
) => {
369-
return new AtlasUserData(
370-
getTestSchema(validatorOpts),
371-
type,
369+
return new AtlasUserData(getTestSchema(validatorOpts), type, {
372370
orgId,
373371
projectId,
374-
getResourceUrlStub,
375-
authenticatedFetchStub,
376-
{}
377-
);
372+
getResourceUrl: getResourceUrlStub,
373+
authenticatedFetch: authenticatedFetchStub,
374+
});
378375
};
379376

380377
const mockResponse = (data: unknown, ok = true, status = 200) => {
@@ -415,8 +412,10 @@ describe('AtlasUserData', function () {
415412
expect(new Date(body.createdAt as string)).to.be.instanceOf(Date);
416413
});
417414

418-
it('returns false when response is not ok', async function () {
419-
authenticatedFetchStub.resolves(mockResponse({}, false, 500));
415+
it('returns false when authenticatedFetch throws an error', async function () {
416+
authenticatedFetchStub.rejects(
417+
new Error('HTTP 500: Internal Server Error')
418+
);
420419
getResourceUrlStub.resolves(
421420
'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
422421
);
@@ -449,17 +448,13 @@ describe('AtlasUserData', function () {
449448
'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
450449
);
451450

452-
const userData = new AtlasUserData(
453-
getTestSchema(),
454-
'FavoriteQueries',
455-
'test-org',
456-
'test-proj',
457-
getResourceUrlStub,
458-
authenticatedFetchStub,
459-
{
460-
serialize: (data) => `custom:${JSON.stringify(data)}`,
461-
}
462-
);
451+
const userData = new AtlasUserData(getTestSchema(), 'FavoriteQueries', {
452+
orgId: 'test-org',
453+
projectId: 'test-proj',
454+
getResourceUrl: getResourceUrlStub,
455+
authenticatedFetch: authenticatedFetchStub,
456+
serialize: (data) => `custom:${JSON.stringify(data)}`,
457+
});
463458

464459
await userData.write('test-id', { name: 'Custom' });
465460

@@ -489,8 +484,8 @@ describe('AtlasUserData', function () {
489484
expect(options.method).to.equal('DELETE');
490485
});
491486

492-
it('returns false when response is not ok', async function () {
493-
authenticatedFetchStub.resolves(mockResponse({}, false, 404));
487+
it('returns false when authenticatedFetch throws an error', async function () {
488+
authenticatedFetchStub.rejects(new Error('HTTP 404: Not Found'));
494489
getResourceUrlStub.resolves(
495490
'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
496491
);
@@ -583,8 +578,10 @@ describe('AtlasUserData', function () {
583578
expect(result.errors[0].message).to.equal('Unknown error');
584579
});
585580

586-
it('handles non-ok response gracefully', async function () {
587-
authenticatedFetchStub.resolves(mockResponse({}, false, 500));
581+
it('handles authenticatedFetch errors gracefully', async function () {
582+
authenticatedFetchStub.rejects(
583+
new Error('HTTP 500: Internal Server Error')
584+
);
588585
getResourceUrlStub.resolves(
589586
'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
590587
);
@@ -594,7 +591,9 @@ describe('AtlasUserData', function () {
594591

595592
expect(result.data).to.have.lengthOf(0);
596593
expect(result.errors).to.have.lengthOf(1);
597-
expect(result.errors[0].message).to.contain('Failed to get data: 500');
594+
expect(result.errors[0].message).to.contain(
595+
'HTTP 500: Internal Server Error'
596+
);
598597
});
599598

600599
it('uses custom deserializer when provided', async function () {
@@ -604,22 +603,18 @@ describe('AtlasUserData', function () {
604603
'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj'
605604
);
606605

607-
const userData = new AtlasUserData(
608-
getTestSchema(),
609-
'FavoriteQueries',
610-
'test-org',
611-
'test-proj',
612-
getResourceUrlStub,
613-
authenticatedFetchStub,
614-
{
615-
deserialize: (data) => {
616-
if (data.startsWith('custom:')) {
617-
return JSON.parse(data.slice(7));
618-
}
619-
return JSON.parse(data);
620-
},
621-
}
622-
);
606+
const userData = new AtlasUserData(getTestSchema(), 'FavoriteQueries', {
607+
orgId: 'test-org',
608+
projectId: 'test-proj',
609+
getResourceUrl: getResourceUrlStub,
610+
authenticatedFetch: authenticatedFetchStub,
611+
deserialize: (data) => {
612+
if (data.startsWith('custom:')) {
613+
return JSON.parse(data.slice(7));
614+
}
615+
return JSON.parse(data);
616+
},
617+
});
623618

624619
const result = await userData.readAll();
625620

@@ -705,7 +700,7 @@ describe('AtlasUserData', function () {
705700
expect(putOptions.headers['Content-Type']).to.equal('application/json');
706701
});
707702

708-
it('returns false when response is not ok', async function () {
703+
it('returns false when authenticatedFetch throws an error', async function () {
709704
const getResponse = {
710705
data: JSON.stringify({ name: 'Original Name', hasDarkMode: true }),
711706
};
@@ -714,7 +709,7 @@ describe('AtlasUserData', function () {
714709
.onFirstCall()
715710
.resolves(mockResponse(getResponse))
716711
.onSecondCall()
717-
.resolves(mockResponse({}, false, 400));
712+
.rejects(new Error('HTTP 400: Bad Request'));
718713

719714
getResourceUrlStub
720715
.onFirstCall()
@@ -756,17 +751,13 @@ describe('AtlasUserData', function () {
756751
'cluster-connection.cloud.mongodb.com/FavoriteQueries/test-org/test-proj/test-id'
757752
);
758753

759-
const userData = new AtlasUserData(
760-
getTestSchema(),
761-
'FavoriteQueries',
762-
'test-org',
763-
'test-proj',
764-
getResourceUrlStub,
765-
authenticatedFetchStub,
766-
{
767-
serialize: (data) => `custom:${JSON.stringify(data)}`,
768-
}
769-
);
754+
const userData = new AtlasUserData(getTestSchema(), 'FavoriteQueries', {
755+
orgId: 'test-org',
756+
projectId: 'test-proj',
757+
getResourceUrl: getResourceUrlStub,
758+
authenticatedFetch: authenticatedFetchStub,
759+
serialize: (data) => `custom:${JSON.stringify(data)}`,
760+
});
770761

771762
await userData.updateAttributes('test-id', { name: 'Updated' });
772763

packages/compass-user-data/src/user-data.ts

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export type FileUserDataOptions<Input> = {
2323
};
2424

2525
export type AtlasUserDataOptions<Input> = {
26+
orgId: string;
27+
projectId: string;
28+
getResourceUrl: GetResourceUrl;
29+
authenticatedFetch: AuthenticatedFetch;
2630
serialize?: SerializeContent<Input>;
2731
deserialize?: DeserializeContent;
2832
};
@@ -274,11 +278,14 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
274278
constructor(
275279
validator: T,
276280
dataType: string,
277-
orgId: string,
278-
projectId: string,
279-
getResourceUrl: GetResourceUrl,
280-
authenticatedFetch: AuthenticatedFetch,
281-
{ serialize, deserialize }: AtlasUserDataOptions<z.input<T>>
281+
{
282+
orgId,
283+
projectId,
284+
getResourceUrl,
285+
authenticatedFetch,
286+
serialize,
287+
deserialize,
288+
}: AtlasUserDataOptions<z.input<T>>
282289
) {
283290
super(validator, dataType, { serialize, deserialize });
284291
this.authenticatedFetch = authenticatedFetch;
@@ -288,32 +295,25 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
288295
}
289296

290297
async write(id: string, content: z.input<T>): Promise<boolean> {
298+
const url = await this.getResourceUrl(
299+
`${this.dataType}/${this.orgId}/${this.projectId}`
300+
);
301+
291302
try {
292303
this.validator.parse(content);
293304

294-
const response = await this.authenticatedFetch(
295-
await this.getResourceUrl(
296-
`${this.dataType}/${this.orgId}/${this.projectId}`
297-
),
298-
{
299-
method: 'POST',
300-
headers: {
301-
'Content-Type': 'application/json',
302-
},
303-
body: JSON.stringify({
304-
id: id,
305-
data: this.serialize(content),
306-
createdAt: new Date(),
307-
projectId: this.projectId,
308-
}),
309-
}
310-
);
311-
312-
if (!response.ok) {
313-
throw new Error(
314-
`Failed to post data: ${response.status} ${response.statusText}`
315-
);
316-
}
305+
await this.authenticatedFetch(url, {
306+
method: 'POST',
307+
headers: {
308+
'Content-Type': 'application/json',
309+
},
310+
body: JSON.stringify({
311+
id: id,
312+
data: this.serialize(content),
313+
createdAt: new Date(),
314+
projectId: this.projectId,
315+
}),
316+
});
317317

318318
return true;
319319
} catch (error) {
@@ -322,9 +322,7 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
322322
'Atlas Backend',
323323
'Error writing data',
324324
{
325-
url: await this.getResourceUrl(
326-
`${this.dataType}/${this.orgId}/${this.projectId}`
327-
),
325+
url,
328326
error: (error as Error).message,
329327
}
330328
);
@@ -333,30 +331,22 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
333331
}
334332

335333
async delete(id: string): Promise<boolean> {
334+
const url = await this.getResourceUrl(
335+
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
336+
);
337+
336338
try {
337-
const response = await this.authenticatedFetch(
338-
await this.getResourceUrl(
339-
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
340-
),
341-
{
342-
method: 'DELETE',
343-
}
344-
);
345-
if (!response.ok) {
346-
throw new Error(
347-
`Failed to delete data: ${response.status} ${response.statusText}`
348-
);
349-
}
339+
await this.authenticatedFetch(url, {
340+
method: 'DELETE',
341+
});
350342
return true;
351343
} catch (error) {
352344
log.error(
353345
mongoLogId(1_001_000_367),
354346
'Atlas Backend',
355347
'Error deleting data',
356348
{
357-
url: await this.getResourceUrl(
358-
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
359-
),
349+
url,
360350
error: (error as Error).message,
361351
}
362352
);
@@ -378,11 +368,6 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
378368
method: 'GET',
379369
}
380370
);
381-
if (!response.ok) {
382-
throw new Error(
383-
`Failed to get data: ${response.status} ${response.statusText}`
384-
);
385-
}
386371
const json = await response.json();
387372
for (const item of json) {
388373
try {
@@ -410,7 +395,7 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
410395
...data,
411396
};
412397

413-
const response = await this.authenticatedFetch(
398+
await this.authenticatedFetch(
414399
await this.getResourceUrl(
415400
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
416401
),
@@ -422,11 +407,6 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
422407
body: this.serialize(newData),
423408
}
424409
);
425-
if (!response.ok) {
426-
throw new Error(
427-
`Failed to update data: ${response.status} ${response.statusText}`
428-
);
429-
}
430410
return true;
431411
} catch (error) {
432412
log.error(
@@ -446,20 +426,14 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
446426

447427
// TODO: change this depending on whether or not updateAttributes can provide all current data
448428
async readOne(id: string): Promise<z.output<T>> {
429+
const url = await this.getResourceUrl(
430+
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
431+
);
432+
449433
try {
450-
const getResponse = await this.authenticatedFetch(
451-
await this.getResourceUrl(
452-
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
453-
),
454-
{
455-
method: 'GET',
456-
}
457-
);
458-
if (!getResponse.ok) {
459-
throw new Error(
460-
`Failed to fetch data: ${getResponse.status} ${getResponse.statusText}`
461-
);
462-
}
434+
const getResponse = await this.authenticatedFetch(url, {
435+
method: 'GET',
436+
});
463437
const json = await getResponse.json();
464438
const data = this.validator.parse(this.deserialize(json.data as string));
465439
return data;
@@ -469,9 +443,7 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
469443
'Atlas Backend',
470444
'Error reading data',
471445
{
472-
url: await this.getResourceUrl(
473-
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
474-
),
446+
url,
475447
error: (error as Error).message,
476448
}
477449
);

0 commit comments

Comments
 (0)