Git Product home page Git Product logo

koa-transform-media-url's Introduction

The koa-transform-media-url is a koa middleware. It's used to transform media link to id.

Installation

install packages

npm install @vahaha/koa-transform-media-url
// yarn install @vahaha/koa-transform-media-url

Usage

Setting

The first setting is called once time when start programming.

function fnGetBaseUrls() {
    return [
        'https://s3.amazonaws.com/package-name',
        'https://cdn.example.com'
    ]
}

function fnGetUrlById(ids = []) {
    return ids.map(id => 'https://s3.amazonaws.com/bucket-name/' + id + '.jpg')
}

function fnGetIdByPath(paths = []) {
    return paths.map(path => path.replace('.jpg', ''))
}

require('@vahaha/koa-transform-media-url')
    .init({ fnGetBaseUrls, fnGetUrlById, fnGetIdByPath }) // init is a async function.

Using in declaring endpoint

const transformer = require('@vahaha/koa-transform-media-url')
const Router = require('@koa/router')
const ctrl = require('./controller')

const router = new Router()

router.post('/files', transformer.transformRequest({
    responseFields: ['fieldName', 'urlInContent'],
    fnGetIdByPaths
}), ctrl.createFile)

router.get('/files', transformer.transformResponse({
    responseFields: ['link', 'urlInContent'],
    fnGetUrlByIds
}), ctrl.getFiles)

// or
// router.use(transformer.transformBoth({
//    requestFields: ['fieldName', 'urlInContent'],
//    responseFields: ['link', 'urlInContent']
//    fnGetIdByPaths,
//    fnGetUrlByIds
// }))

/*
ex: Transform request (body)
--------------------------------
input:
{
    fieldName: 'https://s3.amazonaws.com/bucket-name/e4630b7d-18c2-4d87-adb7-5e7dccc5d633/image/0d64132a-d58a-48f7-907f-0fd93a4a0bff.jpg',
    urlInContent: 'Example image <img src="http://s3.amazonaws.com/bucket-name/e4630b7d-18c2-4d87-adb7-5e7dccc5d633/image/0d64132a-d58a-48f7-907f-0fd93a4a0bff.jpg">',
    otherField: 'value'
}

output:
{
    fieldName: ':media@app://625d32c61a0d682cbb64ff0c',
    urlInContent: 'Example image <img src=":media@app://625d32c61a0d682cbb64ff0c">',
    otherField: 'value'
}

ex: Transform response (body)
--------------------------------
input:
{
    link: ':media@app://625d32c61a0d682cbb64ff0c',
    urlInContent: 'Example image <img src=":media@app://625d32c61a0d682cbb64ff0c">',
    otherField: 'value'
}

output:
{
    link: 'https://s3.amazonaws.com/bucket-name/e4630b7d-18c2-4d87-adb7-5e7dccc5d633/image/0d64132a-d58a-48f7-907f-0fd93a4a0bff.jpg',
    urlInContent: 'Example image <img src="http://s3.amazonaws.com/bucket-name/e4630b7d-18c2-4d87-adb7-5e7dccc5d633/image/0d64132a-d58a-48f7-907f-0fd93a4a0bff.jpg">',
    otherField: 'value'
}
*/

koa-transform-media-url's People

Contributors

vahaha avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.