Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/rest-update-buildheaders-variables-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/rest": patch
---

fix(rest): type `update.buildHeaders` params with `UpdateParams<any>`

The `update.buildHeaders` option in `createDataProvider` was typed as `BuildHeaders<UpdateParams>`, which resolves `variables` to the default `{}` instead of `any`. This was inconsistent with every other `update` option (`getEndpoint`, `buildQueryParams`, `buildBodyParams`, `mapResponse`, `transformError`) and with the default implementation, all of which use `UpdateParams<any>`. As a result, a custom `update.buildHeaders` callback could not read fields off `params.variables` without a type error. Aligned the type with the rest of the `update` options.
2 changes: 1 addition & 1 deletion packages/rest/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type CreateDataProviderOptions = {
update?: {
getEndpoint?: GetEndpoint<UpdateParams<any>>;
getRequestMethod?: (params: UpdateParams<any>) => "put" | "patch";
buildHeaders?: BuildHeaders<UpdateParams>;
buildHeaders?: BuildHeaders<UpdateParams<any>>;

buildQueryParams?: BuildQueryParams<UpdateParams<any>>;

Expand Down
25 changes: 25 additions & 0 deletions packages/rest/test/methods/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ describe("update", () => {
});
});

describe("custom buildHeaders", () => {
it("should derive a header from params.variables", async () => {
nock(API_URL)
.matchHeader("x-from-variables", "bar")
.patch("/update/1", variables)
.reply(200, response);

const { dataProvider } = createDataProvider(API_URL, {
update: {
buildHeaders: async (params) => ({
"x-from-variables": params.variables.foo,
}),
},
});

const result = await dataProvider.update({
id: 1,
resource: "update",
variables,
});

expect(result).toEqual({ data: response });
});
});

describe("failure", () => {
it("should throw HttpError with message and statusCode", async () => {
nock(API_URL).patch("/update/1", variables).reply(400, {
Expand Down