當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript GuardFactory.create方法代碼示例

本文整理匯總了TypeScript中response/guards/guardFactory.GuardFactory.create方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript GuardFactory.create方法的具體用法?TypeScript GuardFactory.create怎麽用?TypeScript GuardFactory.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在response/guards/guardFactory.GuardFactory的用法示例。


在下文中一共展示了GuardFactory.create方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: check

 /**
  * Throws Forbidden error if user is not defendant in the case
  *
  * @returns {express.RequestHandler} - request handler middleware
  */
 static check (): express.RequestHandler {
   return GuardFactory.create((res: express.Response) => {
     const claim: Claim = res.locals.claim
     const user: User = res.locals.user
     return claim.defendantId === user.id
   }, (req: express.Request, res: express.Response): void => {
     throw new ForbiddenError()
   })
 }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:14,代碼來源:onlyDefendantLinkedToClaimCanDoIt.ts

示例2: checkClaimantResponseDoesNotExist

 static checkClaimantResponseDoesNotExist (): express.RequestHandler {
   const allowed = (res: express.Response) => {
     const claim: Claim = res.locals.claim
     return claim.claimantResponse === undefined
   }
   const accessDeniedCallback = (req: express.Request, res: express.Response) => {
     logger.warn('State guard: claimant response already exists - redirecting to dashboard')
     res.redirect(DashboardPaths.dashboardPage.uri)
   }
   return GuardFactory.create(allowed, accessDeniedCallback)
 }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:11,代碼來源:claimantResponseGuard.ts

示例3: requestHandler

  /**
   * Guard checks whether Statement of Means is required.
   * If so it accepts request, otherwise it makes redirect to task list
   *
   * @returns {e.RequestHandler}
   */
  static requestHandler (requireInitiatedModel: boolean = true): express.RequestHandler {
    return GuardFactory.create((res: express.Response) => {
      const draft: Draft<ResponseDraft> = res.locals.responseDraft
      const claim: Claim = res.locals.claim

      return StatementOfMeansFeature.isApplicableFor(claim, draft.document)
        && (requireInitiatedModel ? draft.document.statementOfMeans !== undefined : true)
    }, (req: express.Request, res: express.Response): void => {
      res.redirect(Paths.taskListPage.evaluateUri({ externalId: UUIDUtils.extractFrom(req.path) }))
    })
  }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:17,代碼來源:statementOfMeansStateGuard.ts

示例4: checkResponseExists

 /**
  * Protects response journey from being accessed when response has not been submitted yet. Request in such scenario
  * will result in rendering not found page. In opposite scenario where response has already been made,
  * the request will be processed.
  */
 static checkResponseExists (): express.RequestHandler {
   const allowed = (res: express.Response) => {
     const claim: Claim = res.locals.claim
     return claim.response !== undefined
   }
   const accessDeniedCallback = (req: express.Request, res: express.Response) => {
     logger.warn('State guard: no response exists - rendering not found page')
     throw new NotFoundError(req.path)
   }
   return GuardFactory.create(allowed, accessDeniedCallback)
 }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:16,代碼來源:responseGuard.ts

示例5: requestHandler

  static requestHandler (): express.RequestHandler {

    function isRequestAllowed (res: express.Response): boolean {
      const draft: Draft<ResponseDraft> = res.locals.responseDraft

      return draft.document.isResponseRejected()
    }

    function accessDeniedCallback (req: express.Request, res: express.Response): void {
      logger.warn('Full Rejection Guard: user tried to access page for full rejection flow')
      throw new NotFoundError(req.path)
    }

    return GuardFactory.create(isRequestAllowed, accessDeniedCallback)
  }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:15,代碼來源:fullRejectionGuard.ts

示例6: requestHandler

  static requestHandler (): express.RequestHandler {

    function isRequestAllowed (res: express.Response): boolean {
      const draft: Draft<ResponseDraft> = res.locals.responseDraft
      const claim: Claim = res.locals.claim

      return ClaimFeatureToggles.isFeatureEnabledOnClaim(claim) && draft.document.isResponsePartiallyAdmitted()
    }

    function accessDeniedCallback (req: express.Request, res: express.Response): void {
      const claim: Claim = res.locals.claim
      logger.warn('Partial Admission Guard: user tried to access page for partial admission flow')
      res.redirect(Paths.responseTypePage.evaluateUri({ externalId: claim.externalId }))
    }

    return GuardFactory.create(isRequestAllowed, accessDeniedCallback)
  }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:17,代碼來源:partialAdmissionGuard.ts

示例7: buildRouter

  buildRouter (path: string, ...guards: express.RequestHandler[]): express.Router {
    const stateGuardRequestHandler: express.RequestHandler = GuardFactory.create((res: express.Response): boolean => {
      const model: PaymentIntention = this.createModelAccessor().get(res.locals.draft.document)

      return model
        && model.paymentOption
        && model.paymentOption.isOfType(PaymentType.INSTALMENTS)
    }, (req: express.Request, res: express.Response): void => {
      throw new NotFoundError(req.path)
    })

    return express.Router()
      .get(path + Paths.paymentPlanPage.uri,
        ...guards,
        stateGuardRequestHandler,
        ErrorHandling.apply(async (req: express.Request, res: express.Response) => {
          this.renderView(new Form(this.createModelAccessor().get(res.locals.draft.document).paymentPlan), res)
        }))
      .post(path + Paths.paymentPlanPage.uri,
        ...guards,
        stateGuardRequestHandler,
        FormValidator.requestHandler(PaymentPlanModel, PaymentPlanModel.fromObject, undefined, ['calculatePaymentPlan']),
        ErrorHandling.apply(
          async (req: express.Request, res: express.Response): Promise<void> => {
            let form: Form<PaymentPlanModel> = req.body

            const error: FormValidationError = this.postValidation(req, res)

            if (error) {
              form = new Form<PaymentPlanModel>(form.model, [error, ...form.errors])
            }

            if (form.hasErrors() || _.get(req, 'body.action.calculatePaymentPlan')) {
              this.renderView(form, res)
            } else {
              this.createModelAccessor().patch(res.locals.draft.document, model => model.paymentPlan = form.model)

              await this.saveDraft(res.locals)

              res.redirect(this.buildPostSubmissionUri(req, res))
            }
          }))
  }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:43,代碼來源:payment-plan.ts

示例8: check

  static check (): express.RequestHandler {
    return GuardFactory.create((res: express.Response) => {
      const claim: Claim = res.locals.claim
      const user: User = res.locals.user

      if (claim.settlementReachedAt) {
        logger.warn('State guard: offer settlement reached, redirecting to dashboard')
        return false
      } else if (user.id === claim.claimantId && claim.claimantId !== claim.defendantId
                && claim.settlement.isOfferResponded()) {
        logger.warn('State guard: offer already accepted, redirecting to dashboard')
        return false
      } else {
        return true
      }
    }, (req: express.Request, res: express.Response) => {
      res.redirect(Paths.dashboardPage.uri)
    })
  }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:19,代碼來源:offerAcceptedGuard.ts

示例9: buildRouter

  buildRouter (path: string, ...guards: express.RequestHandler[]): express.Router {
    const stateGuardRequestHandler: express.RequestHandler = GuardFactory.create((res: express.Response): boolean => {
      const model: PaymentIntention = this.createModelAccessor().get(res.locals.draft.document)

      return model && model.paymentOption && model.paymentOption.isOfType(PaymentType.BY_SET_DATE)
    }, (req: express.Request): void => {
      throw new NotFoundError(req.path)
    })

    return express.Router()
      .get(
        path + Paths.paymentDatePage.uri,
        ...guards,
        stateGuardRequestHandler,
        (req: express.Request, res: express.Response) => {
          this.renderView(new Form(this.createModelAccessor().get(res.locals.draft.document).paymentDate), res)
        })
      .post(
        path + Paths.paymentDatePage.uri,
        ...guards,
        stateGuardRequestHandler,
        FormValidator.requestHandler(PaymentDate, PaymentDate.fromObject),
        ErrorHandling.apply(async (req: express.Request, res: express.Response) => {
          const form: Form<PaymentDate> = req.body

          if (form.hasErrors()) {
            this.renderView(form, res)
          } else {
            this.createModelAccessor().patch(res.locals.draft.document, model => model.paymentDate = form.model)

            await this.saveDraft(res.locals)

            res.redirect(this.buildPostSubmissionUri(req, res))
          }
        }))
  }
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:36,代碼來源:payment-date.ts

示例10: renderView

import { PartyType } from 'common/partyType'

import { ErrorHandling } from 'shared/errorHandling'
import { DraftService } from 'services/draftService'
import { Claim } from 'claims/models/claim'
import { DraftCCJ } from 'ccj/draft/draftCCJ'
import { Draft } from '@hmcts/draft-store-client'

import { Logger } from '@hmcts/nodejs-logging'

const logger = Logger.getLogger('ccj/guards/individualDateOfBirth')

const accessGuardRequestHandler: express.RequestHandler = GuardFactory.create((res: express.Response): boolean => {
  const claim: Claim = res.locals.claim

  return claim.claimData.defendant.type === PartyType.INDIVIDUAL.value
}, (req: express.Request, res: express.Response): void => {
  logger.warn(`CCJ state guard: defendant date of birth is only available for individual defendants - redirecting to dashboard page`)
  res.redirect(DashboardPaths.dashboardPage.uri)
})

function renderView (form: Form<DateOfBirth>, res: express.Response): void {
  res.render(Paths.dateOfBirthPage.associatedView, { form: form })
}

/* tslint:disable:no-default-export */
export default express.Router()
  .get(Paths.dateOfBirthPage.uri,
    accessGuardRequestHandler,
    (req: express.Request, res: express.Response) => {
      const draft: Draft<DraftCCJ> = res.locals.ccjDraft
      renderView(new Form(draft.document.defendantDateOfBirth), res)
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:32,代碼來源:date-of-birth.ts


注:本文中的response/guards/guardFactory.GuardFactory.create方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。