Skip to content

Commit

Permalink
feat: Update user API to support partial updates
Browse files Browse the repository at this point in the history
This commit updates the user API to support partial updates. It adds a new custom service call `patchUser` that allows updating specific fields of a user object instead of sending the entire object. The `partialUserShape` is introduced to define the shape of the partial user object.

Code changes:
- Added `partialUserShape` to `user/models.ts`
- Added `patchUser` custom service call to `userApi` in `user/api.ts`
  • Loading branch information
noriega2112 committed Aug 29, 2024
1 parent a2a40e7 commit 4146161
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createApi, createCustomServiceCall } from '@thinknimble/tn-models'
import { z } from 'zod'
import { axiosInstance } from '../axios-instance'
import { forgotPasswordShape, loginShape, userCreateShape, userShape } from './models'
import { forgotPasswordShape, loginShape, partialUserShape, userCreateShape, userShape } from './models'

const login = createCustomServiceCall({
inputShape: loginShape,
Expand Down Expand Up @@ -34,12 +34,22 @@ const logout = createCustomServiceCall(async ({ client }) => {
return client.post(`/logout/`)
})

const patchUser = createCustomServiceCall({
inputShape: partialUserShape,
outputShape: userShape,
cb: async ({ client, slashEndingBaseUri, input, utils }) => {
const { id, ...body } = utils.toApi(input)
const res = await client.put(`${slashEndingBaseUri}${id}/`, body)
return utils.fromApi(res.data)
},
})

export const userApi = createApi({
client: axiosInstance,
baseUri: '/users/',
models: {
create: userCreateShape,
entity: userShape,
},
customCalls: { login, requestPasswordResetCode, resetPassword, logout },
customCalls: { login, requestPasswordResetCode, resetPassword, logout, patchUser },
})
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ export const loginShape = {
}

export type LoginShape = GetInferredFromRaw<typeof loginShape>

type ZodOptionalRecord<T extends z.ZodRawShape> = {
[TKey in keyof T]: z.ZodOptional<T[TKey]>
}

export const zodPartialize = <T extends z.ZodRawShape>(zShape: T) => {
return Object.fromEntries(
Object.entries(zShape).map(([k, v]) => {
return [k, v.optional()]
}),
) as ZodOptionalRecord<T>
}

export const fullNameZod = z.string().refine(
(value) => {
return value.split(' ').filter(Boolean).length >= 2
},
{ message: 'Please provide a full name (first and last name)' },
)

export const partialUserShape = { ...zodPartialize(userShape), id: z.string().uuid() }

0 comments on commit 4146161

Please sign in to comment.