Skip to content

Commit e88856f

Browse files
committed
stricter typing for type param and fix update tests
1 parent 31a8643 commit e88856f

File tree

2 files changed

+33
-47
lines changed

2 files changed

+33
-47
lines changed

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ describe('AtlasUserData', function () {
440440
validatorOpts: ValidatorOptions = {},
441441
orgId = 'test-org',
442442
groupId = 'test-group',
443-
type = 'favorites'
443+
type: 'favorites' | 'recents' | 'pipelines' = 'favorites'
444444
) => {
445445
return new AtlasUserData(
446446
getTestSchema(validatorOpts),
@@ -613,7 +613,7 @@ describe('AtlasUserData', function () {
613613
const result = await userData.readAll();
614614

615615
expect(result.data).to.have.lengthOf(0);
616-
expect(result.errors).to.have.lengthOf(0);
616+
expect(result.errors).to.have.lengthOf(1);
617617
});
618618

619619
it('handles errors gracefully', async function () {
@@ -703,11 +703,7 @@ describe('AtlasUserData', function () {
703703
hasDarkMode: false,
704704
});
705705

706-
expect(result).to.deep.equal({
707-
...defaultValues(),
708-
name: 'Updated Name',
709-
hasDarkMode: false,
710-
});
706+
expect(result).equals(true);
711707

712708
expect(authenticatedFetchStub).to.have.been.calledOnce;
713709
const [url, options] = authenticatedFetchStub.firstCall.args;
@@ -718,18 +714,15 @@ describe('AtlasUserData', function () {
718714
expect(options.headers['Content-Type']).to.equal('application/json');
719715
});
720716

721-
it('throws error when response is not ok', async function () {
717+
it('returns false when response is not ok', async function () {
722718
authenticatedFetchStub.resolves(mockResponse({}, false, 400));
723719

724720
const userData = getAtlasUserData();
725721

726-
try {
727-
await userData.updateAttributes('test-id', { name: 'Updated' });
728-
expect.fail('Should have thrown error');
729-
} catch (error) {
730-
expect(error).to.be.instanceOf(Error);
731-
expect((error as Error).message).to.contain('Failed to update data');
732-
}
722+
const result = await userData.updateAttributes('test-id', {
723+
name: 'Updated',
724+
});
725+
expect(result).equals(false);
733726
});
734727

735728
it('uses custom serializer for request body', async function () {
@@ -807,12 +800,12 @@ describe('AtlasUserData', function () {
807800
it('constructs URL correctly for different types', async function () {
808801
authenticatedFetchStub.resolves(mockResponse({}));
809802

810-
const userData = getAtlasUserData({}, 'org123', 'group456', 'recent');
803+
const userData = getAtlasUserData({}, 'org123', 'group456', 'recents');
811804
await userData.write('item789', { name: 'Recent Item' });
812805

813806
const [url] = authenticatedFetchStub.firstCall.args;
814807
expect(url).to.equal(
815-
'cluster-connection.cloud-local.mongodb.com/recent/org123/group456'
808+
'cluster-connection.cloud-local.mongodb.com/recents/org123/group456'
816809
);
817810
});
818811
});

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
342342
private readonly authenticatedFetch;
343343
private orgId: string = '';
344344
private groupId: string = '';
345-
private readonly type: string;
345+
private readonly type: 'recents' | 'favorites' | 'pipelines';
346346
private readonly BASE_URL = 'cluster-connection.cloud-local.mongodb.com';
347347
constructor(
348348
validator: T,
@@ -352,7 +352,7 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
352352
) => Promise<Response>,
353353
orgId: string,
354354
groupId: string,
355-
type: string,
355+
type: 'recents' | 'favorites' | 'pipelines',
356356
{ serialize, deserialize }: AtlasUserDataOptions<z.input<T>>
357357
) {
358358
super(validator, { serialize, deserialize });
@@ -426,40 +426,33 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
426426
}
427427

428428
async readAll(): Promise<ReadAllResult<T>> {
429-
const response = await this.authenticatedFetch(this.getUrl(), {
430-
method: 'GET',
431-
});
432429
const result: ReadAllResult<T> = {
433430
data: [],
434431
errors: [],
435432
};
436-
if (!response.ok) {
437-
log.error(
438-
mongoLogId(1_001_000_364),
439-
'Atlas Backend',
440-
'Error reading data',
441-
{
442-
url: this.getUrl(),
443-
error: `Status ${response.status} ${response.statusText}`,
444-
}
445-
);
446-
result.errors.push(
447-
new Error(
433+
try {
434+
const response = await this.authenticatedFetch(this.getUrl(), {
435+
method: 'GET',
436+
});
437+
if (!response.ok) {
438+
throw new Error(
448439
`Failed to get data: ${response.status} ${response.statusText}`
449-
)
450-
);
451-
return result;
452-
}
453-
const json = await response.json();
454-
for (const item of json) {
455-
try {
456-
const parsedData = this.deserialize(item.data as string);
457-
result.data.push(this.validator.parse(parsedData) as z.output<T>);
458-
} catch (error) {
459-
result.errors.push(error as Error);
440+
);
441+
}
442+
const json = await response.json();
443+
for (const item of json) {
444+
try {
445+
const parsedData = this.deserialize(item.data as string);
446+
result.data.push(this.validator.parse(parsedData) as z.output<T>);
447+
} catch (error) {
448+
result.errors.push(error as Error);
449+
}
460450
}
451+
return result;
452+
} catch (error) {
453+
result.errors.push(error as Error);
454+
return result;
461455
}
462-
return result;
463456
}
464457

465458
async updateAttributes(
@@ -482,7 +475,7 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
482475
return true;
483476
} catch (error) {
484477
log.error(
485-
mongoLogId(1_001_000_365),
478+
mongoLogId(1_001_000_364),
486479
'Atlas Backend',
487480
'Error updating data',
488481
{

0 commit comments

Comments
 (0)