Skip to content

Commit

Permalink
fix: Handle camelCase selection in proxy as an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Jul 31, 2023
1 parent b59bf1c commit df6e78b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/adapters/action/dispatcher-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ export class HttpActionDispatcher implements ActionDispatcher {

switch (action) {
case "lookup":
case "verifyCredentials": {
case "verify_credentials": {

Check warning on line 83 in src/adapters/action/dispatcher-http.ts

View check run for this annotation

Codecov / codecov/patch

src/adapters/action/dispatcher-http.ts#L83

Added line #L83 was not covered by tests
return "fetch";
}
case "updateCredentials": {
case "update_credentials": {

Check warning on line 86 in src/adapters/action/dispatcher-http.ts

View check run for this annotation

Codecov / codecov/patch

src/adapters/action/dispatcher-http.ts#L86

Added line #L86 was not covered by tests
return "update";
}
default: {
Expand Down
23 changes: 21 additions & 2 deletions src/adapters/action/proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("RequestBuilder", () => {
return {} as T;
},
},
["/root"],
["root"],
);
const data = {};
builder.$select("foo").bar.fetch(data);
Expand All @@ -57,7 +57,7 @@ describe("RequestBuilder", () => {
return {} as T;
},
},
["/root"],
["root"],
);
const data = {};
builder.$select("foo").bar.create(data);
Expand All @@ -67,6 +67,25 @@ describe("RequestBuilder", () => {
expect(action?.data).toBe(data);
});

it("builds a resource with CamelCase", () => {
let action: Action | undefined;

const builder: any = createActionProxy(
{
dispatch: async <T>(a: Action) => {
action = a;
return {} as T;
},
},
["root"],
);
const data = {};
builder.$select("AlphaBeta").gammaDelta.create(data);
expect(action?.type).toBe("create");
expect(action?.path).toBe("/root/AlphaBeta/gamma_delta");
expect(action?.data).toBe(data);
});

it("cannot invoke without context", () => {
const builder: any = createActionProxy({
dispatch: Promise.resolve,
Expand Down
10 changes: 8 additions & 2 deletions src/adapters/action/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ const get =
if (typeof property === "symbol") {
return;
}
return createActionProxy<T>(actionDispatcher, [...context, property]);
if (property === "$select") {
return createActionProxy<T>(actionDispatcher, [...context, property]);
}
return createActionProxy<T>(actionDispatcher, [
...context,
snakeCase(property),
]);
};

const apply =
Expand All @@ -61,7 +67,7 @@ const apply =
]);
}

const path = "/" + context.map((name) => snakeCase(name)).join("/");
const path = "/" + context.join("/");
const [data, meta] = args;

return actionDispatcher.dispatch<T>({
Expand Down
12 changes: 12 additions & 0 deletions tests/rest/v1/timelines.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ describe("timeline", () => {
});
});

it("returns hashtag in camel case", () => {
return sessions.use(async (client) => {
const status = await client.rest.v1.statuses.create({
status: "#CamelCase",
});
const statuses = await client.rest.v1.timelines.tag
.$select("CamelCase")
.list();
expect(statuses).toContainId(status.id);
});
});

it("returns list", () => {
return sessions.use(async (client) => {
const list = await client.rest.v1.lists.create({ title: "List" });
Expand Down

0 comments on commit df6e78b

Please sign in to comment.