npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

web-commons-export

v1.2.10

Published

Export library for creating presentations by using server-side rendering

Downloads

2

Readme

web-commons-export

Library for exporting presentations for Nielsen Marketing Cloud

Standard - JavaScript Style Guide

Description

This library contains helpers for generating presentations with minimal effort using server-side rendering techniques. Using this Library, any App in Nielsen Marketing Cloud can create an export server which exposes an endpoint for generating presentations with nielsen theme (logo, background, copyright, etc) and automatic title injection, as well as automatic page counts and more.

Creating presentations

  • Easily create pages with rich UI elements
  • Built-in page counts, title, copyright, background, etc …
  • Custom components version
  • Supports pptx + pdf
  • Decoupled themes repository

Creating export server

  • Initializes backend with minimal effort
  • Automatic Webpack configuration

Flow

Diagram

Requirements

Usage

Creating documents

  1. Create a directory under documents to hold your document. e.g: /src/documents/SegmentDocument/SegmentDocument.js.

the file exports your document creator function and unique document identifier. e.g:

export default {
  documentName: 'segment',
  documentCreator: async function (authorizationHeader) {
    let data = await getDataAsync(authorizationHeader)
    let pages = [Page1, Page2]
    return {
      pages,
      data,
    }
  }
}

add the document into your array of all defined documents in /documents/index.js:

import segmentDocument from './SegmentDocument/SegmentDocument'
export default [
  segmentDocument
]
  1. if required, you can create a dedicated export styles file SegmentDocument.less and import it in SegmentDocument.js.

  2. implement the getDataAsync function to return the data required for the document

async function getDataAsync (authorizationHeader, queryParams) {
  const headers = {
    'authorization': authorizationHeader
  }
  return fetch('http://localhost:6200/dmp/entities', {headers})
    .then(res => res.json())
}
  • authorizationHeader contains the authorization field as passed to the request's header
  • queryParams contains all the query params as passed to the request in the querystring
  1. define the Page classes. each class represents a Page in the presentation you export. use the page decorator for built-in capabilities by the library. e.g:
import { page } from 'web-export/documents'
@page({
    title: 'SEGMENT REPORT', 
    bodyText:'make love not segments', 
    theme: 'NIELSEN_SIDEBAR_AND_N', 
    skipPageNumber: true
})
class Page1 {
  constructor (data) {
    this.data = data
  }

  getMainContent () {
    const data = this.data
    return (
        <SegmentViewer data={data} isExportMode />
    )
  }
}

if you would like to build the presentation via custom code, you can expose getCustomContent method. e.g:

@page({bodyText: 'page 2 text body'})
class Page2 {
  getCustomContent (slide) {
    slide.addText('Title with manual location and design',
     {x: '50%', y: 0.25, font_size: 18, font_face: 'Arial', color: 'FF0000'})
  }

  getMainContent () {
    return (
      <div>
        Did you know? method `getMainContent` is optionally. if you use getCustomContent you may not even need it.
      </div>
    )
  }
}

read here for more details on how to write custom code.

page decorator options

| Option | Type | Default | Description | Possible Values | | :---------------- | :------ | :------------------------ | :-----------------------------------------| :---------------------------- | | title | string | | title in page header | | | bodyText | string | | paragraph below the title | | | skipPageNumber | bool | false | skip the page count | true, false | | theme | string | NIELSEN_SIDEBAR_AND_N | theme (background, logo, copyright, etc) | null, NIELSEN_SIDEBAR_AND_N |

Settings the server

The library exports setServer function which gets an express app and initializes the endpoint /export. the function also receives array of documents and optional array of themes, and registers them.

usage example:

const express = require('express')
const {setServer} = require('web-commons-export/server')
const themes = require('web-commons-export-themes')

let documents = require('./dist/documents')
const app = express()
setServer(app, '/agen/export', documents, themes)
app.listen(9003, () => {
  console.log('Export Server is listening on port 9003 ...')
})

the exporting endpoint is: /export/:documentName with optionally format querystring param. e.g: https://localhost/file/agen/download/export/segment?format=pptx

the library supports the following formats: pptx (default), pdf

Setting authentication for api requests

the export server should implement server-side jwt authentication in order to be able to perform api requests for getting data required for the export. read here for instructions.

Themes

Themes repository

There is an additional library web-commons-export-themes which acts as a decoupled repository of the common themes. when calling setServer initialization function, you pass it the themes from this library as follows:

const themes = require('web-commons-export-themes')
setServer(app, '/agen/export', documentsRegisters, themes)

Creating custom themes

If you would like to use a custom theme unique to your app across documents, you can register a new theme manually. to do so, add it to the themes argument of the setServer function. e.g:

let copyrightTheme = {
  themeId: 'NIELSEN_SIDEBAR_AND_N',
  apply: function (slide) {
      const copyright = `Copyright © ${new Date().getFullYear()} The Nielsen Company (US), LLC. Confidential and proprietary. Do not distribute.`
      slide.addText(copyright, {
        x: -2.73,
        y: 4.4,
        w: 5,
        align: 'r',
        rotate: 270,
        font_size: 6,
        font_face: 'Arial',
        color: 'FFFFFF'
      })
  }
}
setServer(app, '/agen/export', documentsRegisters, [...themes, copyrightTheme])

now you can pass it to any page in the theme property of the page decorator.

note: If you created a theme that can be useful for other NMC apps, please make a PR to the web-commons-export-themes library.

Configuring the server

Webpack

The library exports a common webpack configuration with relevant settings. among other thing it does, is compiling all the styles into a single bundled css file, extracting fonts and images of the app, setting global variable for hinting server mode, setting globals, and more.

the configuration contains common loaders. however, you may extend the configuration or override it for your app's needs. for example, in case you need to transpile sass or TypeScript.

usage:

const exportLibWebpackConfig = require('web-commons-export/server').webpackConfig(__dirname)

module.exports = exportLibWebpackConfig.merge({
  entry: {
    documents: ['babel-polyfill', './src/documents/index.js']
  }
})

Using Infra goodness

When NMC app is loaded, the Infra injects all kind of libraries for it to use before it loads the app, such as: Underscore, React, Moment, WebInfra-Components and more.

since there is no such Infra injection for us in the export server, its our responsibility to simulate that injection for any library required by the document.

The trick is a predefined global variable __IS_SERVER_SIDE__ loaded by webpack that would hint us that we are on server-side, and if that the case, then we inject what required.

in a InfraFacade.js file:

/**
 * Facade for connecting with Infra
 */

let facade
// we run in Server Side
if (__IS_SERVER_SIDE__) {
  // Mock window
  global.window = {}

  // expose underscore from the actual package, because there is no Infra to inject it globally for us
  let underscore = require('underscore')
  global.window._ = underscore
  global._ = underscore

  // import Infra components from the actual package, because there is no Infra to inject it globally for us
  let InfraComponents = require('@master/webinfra-frontend-components')
  facade = {InfraComponents}
}
// we run in browser
else {
  // use facade which exposes the global Infra components injected for us by the Infra globally
  facade = require('webapps-frontend-components/src/main/Infra/InfraFacadeShared')
}

export default facade

now in the app, we can import what required from the facade. e.g:

import { InfraComponents } from 'InfraFacade'
const { CancelButton, DetailsActionButton } = InfraComponents

NMC webapp usage example

https://github.com/xl8/web-agen-frontend/tree/master/agen-app/export-server