Skip to content

Commit

Permalink
new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekz committed Aug 2, 2024
1 parent 1b1b512 commit e02c0d4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 15 deletions.
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
- [ ] claim by serial
- [ ] objekt collection by address
- [x] get token
- [ ] apply lenticular
- [ ] remove lenticular
- [x] apply lenticular
- [x] remove lenticular
- [ ] gas station

## Rekord
Expand Down Expand Up @@ -86,3 +86,4 @@
- [x] get current user
- [x] search users
- [x] get user by nickname
- [x] update device profile
2 changes: 1 addition & 1 deletion src/api/base-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class BaseAPI {
}

// probably shouldn't do this
if (response.status === 204) {
if (response.status === 201 || response.status === 204) {
return true as T;
}

Expand Down
9 changes: 5 additions & 4 deletions src/api/objekt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ import { z } from "zod";
import { BaseAPI } from "./base-api";
import { objektFilterSchema } from "../zod/objekt";
import { AccessTokenMissing } from "../errors";
import { ValidArtist } from "./artist";

export class ObjektAPI extends BaseAPI {
/**
* Get the available objekt filters.
*
* Authentication is required.
*/
async filters() {
async filters(artist?: ValidArtist) {
if (!this.config.accessToken) {
throw new AccessTokenMissing();
}

const params = artist ? `?artistName=${artist}` : "";

return await this.request<{
sorts: Filters.Sort[];
filters: Filters.Filter[];
}>(`/objekt/v2/filters`);
}>(`/objekt/v2/filters${params}`);
}

/**
Expand Down Expand Up @@ -92,7 +95,6 @@ export class ObjektAPI extends BaseAPI {
throw new AccessTokenMissing();
}

throw new Error("not implemented");
return await this.request<boolean>(`/lenticular/v1`, {
method: "POST",
body: JSON.stringify({
Expand All @@ -112,7 +114,6 @@ export class ObjektAPI extends BaseAPI {
throw new AccessTokenMissing();
}

throw new Error("not implemented");
return await this.request<boolean>(`/lenticular/v1/${tokenId}`, {
method: "DELETE",
});
Expand Down
24 changes: 24 additions & 0 deletions src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ export class UserAPI extends BaseAPI {
`/user/v1/by-nickname/${nickname}`
);
}

/**
* Update the current user's device profile.
*
* Authentication is required.
*/
async updateDeviceProfile(profile: User.DeviceProfile) {
if (!this.config.accessToken) {
throw new AccessTokenMissing();
}

return await this.request<boolean>(`/user/v1/me/device-profile`, {
method: "PUT",
body: JSON.stringify(profile),
});
}
}

export namespace User {
Expand All @@ -63,6 +79,7 @@ export namespace User {

export type FollowedArtist = Artist.Artist & {
receivedWelcomeObjekt: boolean;
purchaseCount: number;
assetBalance: {
totalComo: number;
totalObjekt: number;
Expand All @@ -76,6 +93,13 @@ export namespace User {
thumbnail: string;
};
};

export type DeviceProfile = {
locale: string;
country: string;
os: string;
appVersion: string;
};
}

export namespace Search {
Expand Down
16 changes: 8 additions & 8 deletions tests/api/objekt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ describe("ObjektAPI", () => {
});

it("should apply lenticular to two objekts", async () => {
expect(() => {
return cosmo.objekts.applyLenticular("A", "B");
}).rejects.toThrowError("not implemented");
const response = await cosmo.objekts.applyLenticular("A", "B");
expect(response).toEqual(true);
});

it("should remove lenticular from an objekt", async () => {
expect(() => {
return cosmo.objekts.removeLenticular("A");
}).rejects.toThrowError("not implemented");
const response = await cosmo.objekts.removeLenticular("A");
expect(response).toEqual(true);
});
});

Expand Down Expand Up @@ -104,12 +102,14 @@ describe("ObjektAPI", () => {
it("applying lenticular to two objekts should handle unauthorized requests", async () => {
await expect(
cosmo.objekts.applyLenticular("A", "B")
).rejects.toThrowError(new Error("not implemented"));
).rejects.toThrowError(
new UnauthorizedError("missing Authorization header")
);
});

it("removing lenticular from an objekt should handle unauthorized requests", async () => {
await expect(cosmo.objekts.removeLenticular("A")).rejects.toThrowError(
new Error("not implemented")
new UnauthorizedError("missing Authorization header")
);
});
});
Expand Down
34 changes: 34 additions & 0 deletions tests/api/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe("UserAPI", () => {
const response = await cosmo.users.search("example");
expect(response).toEqual(json.search);
});

it("should update the device profile", async () => {
const response = await cosmo.users.updateDeviceProfile({
locale: "en",
country: "nz",
os: "ios",
appVersion: "2.9.0",
});
expect(response).toEqual(true);
});
});

describe("unauthenticated", () => {
Expand All @@ -39,6 +49,17 @@ describe("UserAPI", () => {
new AccessTokenMissing()
);
});

it("updating the device profile should throw an error", async () => {
expect(() =>
cosmo.users.updateDeviceProfile({
locale: "en",
country: "nz",
os: "ios",
appVersion: "2.9.0",
})
).rejects.toThrowError(new AccessTokenMissing());
});
});

describe("invalid token", () => {
Expand All @@ -58,6 +79,19 @@ describe("UserAPI", () => {
new UnauthorizedError("missing Authorization header")
);
});

it("updating the device profile should handle unauthorized requests", async () => {
expect(() =>
cosmo.users.updateDeviceProfile({
locale: "en",
country: "nz",
os: "ios",
appVersion: "2.9.0",
})
).rejects.toThrowError(
new UnauthorizedError("missing Authorization header")
);
});
});

it("should get a user by their nickname", async () => {
Expand Down
9 changes: 9 additions & 0 deletions tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const handlers = [
http.get(cosmo("/user/v1/by-nickname/*"), () =>
HttpResponse.json(json.byNickname)
),
http.put(cosmo("/user/v1/me/device-profile"), () =>
HttpResponse.json(undefined, { status: 204 })
),

// auth
http.post(cosmo("/auth/v1/signin"), () => HttpResponse.json(json.login)),
Expand Down Expand Up @@ -49,6 +52,12 @@ export const handlers = [
http.post(cosmo("/objekt/v1/by-serial/*/claim"), () =>
HttpResponse.json(json.claimBySerial)
),
http.post(cosmo("/lenticular/v1"), () =>
HttpResponse.json(undefined, { status: 201 })
),
http.delete(cosmo("/lenticular/v1/*"), () =>
HttpResponse.json(undefined, { status: 204 })
),
http.get(cosmo("/objekt/v1/token/*"), () => HttpResponse.json(json.token)),

// rewards
Expand Down

0 comments on commit e02c0d4

Please sign in to comment.