# This is a top-level object used to set values for the root `status`,
# `headers`, and `body` properties.
# It is the first branch in an abstract decision tree, which ultimately
# resolves to an object that contains values for its own 'status', 'headers',
# and 'body' properties.
# This object uses a ConditionalResolver to determine the object value based
# on the URL pattern in the request object.
veniaResponse:
    resolver: conditional
    when:
        # Requests to graphql/rest endpoints, the media library, and cache are
        # handled by the top-level 'veniaProxy' object, which is a ProxyResolver
        # that passes the request through to the backing Magento server.
        - matches: request.url.pathname
          pattern: '^/(graphql|rest|media)(/|$)'
          use: veniaProxy
        - matches: request.url.pathname
          pattern: '^/(robots\.txt|favicon\.ico|manifest\.json)'
          use: staticFromRoot
        - matches: fileExtension
          pattern: '(js|json|png|jpg|gif|svg|ico|css|txt)'
          use: veniaStatic
        - matches:  urlResolver.redirect_code
          pattern: '(301|302)'
          use: dynamicRedirect
    default: veniaAppShell

# A FileResolver for serving certain files directly from document root,
# even though they are published to the `static` folder in build assets.
staticFromRoot:
    inline:
        status: 200
        headers:
            resolver: inline
            inline:
                content-type: contentTypeFromExtension
                cache-control:
                    when:
                        - matches: env.NODE_ENV
                          pattern: 'production'
                          use:
                            inline: public, max-age=604800
                    default:
                        inline: no-cache, no-store, must-revalidate
        body:
            resolver: file
            parse:
              inline: text
            encoding:
              inline: binary
            file:
                resolver: template
                engine: mustache
                provide:
                    filename: request.url.pathname
                template:
                    resolver: inline
                    inline: './venia-static/{{ filename }}'

contentTypeFromExtension:
    when:
        - matches: fileExtension
          pattern: '^ico$'
          use:
              inline: image/x-icon
        - matches: fileExtension
          pattern: '^txt$'
          use:
              inline: text/plain
        - matches: fileExtension
          pattern: '^json$'
          use:
              inline: application/json
    default:
        inline: text/html

# Contains the file extension--the part after the dot--of the URL path.
fileExtension:
    resolver: conditional
    when:
        - matches: request.url.pathname
          pattern: '\.(.*)$'
          use: $match.$1
    default:
        inline: ''

# A ProxyResolver object that passes a request to the backend Magento
# server defined in the MAGENTO_BACKEND_URL environment variable.
# An UPWARD server infers this object as a ProxyResolver due to the presence
# of the 'target' property.
veniaProxy:
    resolver: proxy
    target: env.MAGENTO_BACKEND_URL
    # A local Magento install may have SSH configured and untrusted,
    # which is not a major concern for this one origin, especially if
    # containerized. Clients which require trust may proxy through UPWARD.
    ignoreSSLErrors:
      when:
          - matches: env.NODE_ENV
            pattern: 'production'
            use:
              inline: false
      default:
        inline: true

# Page type data for initial render
veniaPageType:
  resolver: inline
  inline:
    data:
      resolver: computed
      type:
        resolver: inline
        inline: pageType
      additional:
        - type: product
          fetch: '__typename,id'
        - type: cms_page
          fetch: 'identifier'
        - type: category
          fetch: 'uid'

# Nonce for page type inline script
veniaPageTypeNonce:
  resolver: inline
  inline:
    nonce:
      resolver: computed
      type:
        resolver: inline
        inline: pageTypeNonce

# Webpack chunks to preload on page based on page type
veniaWebpackChunks:
  resolver: inline
  inline:
    scripts:
      resolver: computed
      type:
        resolver: inline
        inline: webpackChunks

# The veniaAppShell object resolves to a response that returns server-side
# rendered HTML containing the PWA application shell.
# For SEO purposes, the appropriate meta tags in the HTML head element are also
# set based on information about the resource.
# This object uses properties in the top-level 'veniaResponse' object to return
# the appropriate response values.
veniaAppShell:
    resolver: inline
    inline:
        status:
            resolver: inline
            inline: 200
        headers:
            resolver: inline
            inline:
                content-type:
                    inline: text/html
                cache-control:
                    inline: s-maxage=60
        body:
          resolver: template
          engine: mustache
          provide:
            pageType: veniaPageType.data
            pageTypeNonce: veniaPageTypeNonce.nonce
            webpackChunks: veniaWebpackChunks.scripts
          template:
            resolver: file
            file:
              resolver: inline
              inline: './index.html'


# The veniaStatic object is a DirectoryResolver that allows access to the files
# inside the project's compiled './dist' directory.
veniaStatic:
    resolver: directory
    directory:
        resolver: inline
        inline: '.'

# These are no-ops at runtime; nothing refers to these context values in the
# rest of this file. They exist to declare that the files in the `./static`
# directory are required and should be copied into the build assets by the
# UpwardIncludePlugin. Since they are not directly mentioned elsewhere in this
# file or any other upward.yml file in the build, the UpwardIncludePlugin would
# fail to copy them if they were not mentioned here.

# The static directory includes files which don't need to be compiled.
# They are served by the `veniaStatic` DirectoryResolver, along with the
# bundles and other assets, but since that resolver serves the `.` dist
# directory, the UpwardIncludePlugin will not copy it to avoid circular
# dependency. TODO: This is kind of confusing.
veniaStaticIncludes:
    resolver: directory
    directory:
        resolver: inline
        inline: './venia-static'

urlResolver: urlResolverResult.data.route

urlResolverResult:
  resolver: service
  endpoint:
    resolver: url
    baseUrl: env.MAGENTO_BACKEND_URL
    pathname:
      inline: graphql
  method:
    resolver: inline
    inline: POST
  headers:
    resolver: inline
    inline:
      'content-type': 'application/json'
      accept: 'application/json'
  query:
    resolver: inline
    inline: 'query ResolveURL($url: String!) {
        route(url: $url) {
          type
          relative_url
          redirect_code
        }
      }'
  variables:
    resolver: inline
    inline:
      # This is a barestring indicating a context lookup. It resolves to the
      # `path` value in the URL query string of the request, using the builtin
      # `request` context object.
      url: request.url.pathname

dynamicRedirect:
    resolver: inline
    inline:
        status: urlResolver.redirect_code
        headers:
            resolver: inline
            inline:
                content-type:
                    inline: text/html
                cache-control:
                    inline: s-maxage=60
        body:
          resolver: template
          engine: mustache
          provide:
            pageType: veniaPageType.data
            pageTypeNonce: veniaPageTypeNonce.nonce
            webpackChunks: veniaWebpackChunks.scripts
          template:
            resolver: file
            file:
              resolver: inline
              inline: './index.html'