How to use the sp2.$bind function in sp2

To help you get started, we’ve selected a few sp2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github phenyl / phenyl / modules / utils / src / visit-entities-in-response-data.ts View on Github external
if (resData.payload.entity == null) return resData;
      // @ts-ignore
      const newEntity = visitor(resData.payload.entity);

      const { $set, $docPath } = $bind();
      // @ts-ignore
      if (newEntity === resData.payload.entity) return resData;
      // @ts-ignore
      return update(resData, $set($docPath("payload", "entity"), newEntity));
    }

    case "login": {
      if (resData.payload.user == null) return resData;
      const newUser = visitor(resData.payload.user);
      if (newUser === resData.payload.user) return resData;
      const { $set, $docPath } = $bind();
      return update(resData, $set($docPath("payload", "user"), newUser));
    }

    case "insertOne":
    case "insertMulti":
    case "updateById":
    case "updateMulti":
    case "delete":
    case "logout": {
      return resData;
    }

    default:
      return resData;
  }
}
github phenyl / phenyl / modules / standards / src / encrypt-password-in-request-data.ts View on Github external
export function encryptPasswordInRequestData(
  reqData: GeneralUserEntityRequestData,
  passwordPropName: DocumentPath,
  encrypt: EncryptFunction
): GeneralUserEntityRequestData {
  switch (reqData.method) {
    case "insertOne":
    case "insertAndGet": {
      const { payload } = reqData;
      const { value } = payload;
      const password = getNestedValue(value, passwordPropName);

      if (password) {
        const { $set, $docPath } = $bind();
        const valueWithEncryptedPass = update(
          value,
          // @ts-ignore password always exists
          $set($docPath(passwordPropName), encrypt(password))
        );
        const { $set: $OtherSet, $docPath: $otherDocPath } = $bind<
          typeof reqData
        >();
        return update(
          reqData,
          $OtherSet($otherDocPath("payload", "value"), valueWithEncryptedPass)
        );
      } else {
        return reqData;
      }
    }