Explorar el Código

implement serializeAttachmentSecurely

Yuki Takei hace 1 año
padre
commit
852f9532e3
Se han modificado 1 ficheros con 24 adiciones y 0 borrados
  1. 24 0
      packages/core/src/models/serializers/attachment-serializer.ts

+ 24 - 0
packages/core/src/models/serializers/attachment-serializer.ts

@@ -0,0 +1,24 @@
+import type { IAttachment } from '~/interfaces';
+
+import { isPopulated, type Ref } from '../../interfaces/common';
+
+import { serializeUserSecurely, type IUserSerializedSecurely } from './user-serializer';
+
+export type IAttachmentSerializedSecurely = Omit<IAttachment, 'creator'> & { creator?: Ref<IUserSerializedSecurely> };
+
+export const serializeAttachmentSecurely = (attachment?: Ref<IAttachment>): Ref<IAttachmentSerializedSecurely> | undefined => {
+  // return when it is not a user object
+  if (attachment == null || !isPopulated(attachment)) {
+    return attachment;
+  }
+
+  // serialize User data
+  const { _id, creator, ...restAttachmentProperties } = attachment;
+
+  return {
+    _id,
+    creator: serializeUserSecurely(creator),
+    ...restAttachmentProperties,
+  };
+
+};