-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added specifier for crud functions
- Can now add multiple mapped functions to a create, update, or delete function mapping - update demo to reflect new usage
- Loading branch information
Showing
16 changed files
with
282 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
import { ProfileDocument, UpdateProfileParams } from '@dereekb/demo-firebase'; | ||
import { ProfileDocument, SetProfileUsernameParams, UpdateProfileParams } from '@dereekb/demo-firebase'; | ||
import { DemoUpdateModelfunction } from '../function'; | ||
import { profileForUserRequest } from './profile.util'; | ||
import { userHasNoProfileError } from '../../common'; | ||
|
||
export const updateProfile: DemoUpdateModelfunction<UpdateProfileParams> = async (request) => { | ||
const { nest, auth, data } = request; | ||
const updateProfile = await nest.profileActions.updateProfile(data); | ||
const profileDocument: ProfileDocument = await profileForUserRequest(request); | ||
await updateProfile(profileDocument); | ||
}; | ||
|
||
export const updateProfileUsername: DemoUpdateModelfunction<SetProfileUsernameParams> = async (request) => { | ||
const { nest, auth, data } = request; | ||
const setProfileUsername = await nest.profileActions.setProfileUsername(data); | ||
const profileDocument: ProfileDocument = await profileForUserRequest(request); | ||
const exists = await profileDocument.accessor.exists(); | ||
|
||
if (!exists) { | ||
throw userHasNoProfileError(auth.uid); | ||
} | ||
|
||
await setProfileUsername(profileDocument); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './permission.error'; | ||
export * from './specifier.function'; | ||
export * from './create.model.function'; | ||
export * from './update.model.function'; | ||
export * from './delete.model.function'; |
44 changes: 44 additions & 0 deletions
44
packages/firebase-server/src/lib/nest/model/specifier.function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ModelFirebaseCrudFunctionSpecifier, ModelFirebaseCrudFunctionSpecifierRef, MODEL_FUNCTION_FIREBASE_CRUD_FUNCTION_SPECIFIER_DEFAULT } from '@dereekb/firebase'; | ||
import { objectToMap, PromiseOrValue, serverError } from '@dereekb/util'; | ||
import { NestContextCallableRequestWithAuth } from '../function/nest'; | ||
import { badRequestError } from '../../function'; | ||
|
||
export type OnCallSpecifierHandlerNestContextRequest<N, I = unknown> = NestContextCallableRequestWithAuth<N, I> & ModelFirebaseCrudFunctionSpecifierRef; | ||
export type OnCallSpecifierHandlerFunction<N, I = unknown, O = void> = (request: OnCallSpecifierHandlerNestContextRequest<N, I>) => PromiseOrValue<O>; | ||
|
||
// TODO: Add typings to ensure all expected function keys are present here. | ||
export type OnCallSpecifierHandlerConfig<N> = { | ||
/** | ||
* The default handler function. | ||
*/ | ||
_: OnCallSpecifierHandlerFunction<N, any, any>; | ||
[key: string]: OnCallSpecifierHandlerFunction<N, any, any>; | ||
}; | ||
|
||
export function onCallSpecifierHandler<N>(config: OnCallSpecifierHandlerConfig<N>): OnCallSpecifierHandlerFunction<N> { | ||
const map = objectToMap(config); | ||
|
||
return async (request) => { | ||
const { specifier = MODEL_FUNCTION_FIREBASE_CRUD_FUNCTION_SPECIFIER_DEFAULT } = request; | ||
const handler = map.get(specifier); | ||
|
||
if (handler != null) { | ||
return await handler(request); | ||
} else { | ||
throw unknownModelCrudFunctionSpecifierError(specifier); | ||
} | ||
}; | ||
} | ||
|
||
export function unknownModelCrudFunctionSpecifierError(specifier: ModelFirebaseCrudFunctionSpecifier) { | ||
return badRequestError( | ||
serverError({ | ||
status: 400, | ||
code: 'UNKNOWN_SPECIFIER_ERROR', | ||
message: 'Invalid/unknown specifier for this function.', | ||
data: { | ||
specifier | ||
} | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.