GithubHelp home page GithubHelp logo

voxpelli / node-micropub-express Goto Github PK

View Code? Open in Web Editor NEW
37.0 6.0 12.0 147 KB

Provides a Micropub route for Express 4.x

License: MIT License

JavaScript 100.00%
micropub express indieweb api node-js

node-micropub-express's Introduction

Micropub Express

Build Status Coverage Status dependencies Status Known Vulnerabilities js-semistandard-style FOSSA Status

Provides a Micropub route for Express 4.x

Requirements

Node.js requirement is set in package.json (Use eg. installed-check to validate that it complies with your requirements)

Installation

npm install micropub-express --save

Current status

Early alpha

Supported:

  • Creation of content based items and creation of likes

The rest of the CRUD-operations + other more complex operations are yet to be built and the API might change to adopt to the requirements of those. Versioning will stick to Semantic Versioning to clearly communicate such breaking changes.

Usage

var micropub = require('micropub-express');

// Attach the micropub endpoint to "/micropub" or wherever else you want
app.use('/micropub', micropub({

  // Specify what endpoint you want to verify a token with and what the expected identity returned is
  tokenReference: {
    me: 'http://example.com/',
    endpoint: 'https://tokens.indieauth.com/token',
  },

  // And lastly: Do something with the created micropub document
  handler: function (micropubDocument, req) {
    // Do something with the micropubDocument and return a Promise to communicate status of the handling
    return Promise.resolve().then(function () {
      return { url: 'http://example.com/url/to/new/post' };
    });
  }

}));

Advanced Usage

var express = require('express');
var micropub = require('micropub-express');

var app = express();

// Do some Express magic to support multiple Micropub endpoints in the same application
app.param('targetsite', function (req, res, next, id) {
  // Resolve a token reference from the "targetsite" id and return 404 if you find no match
  if (id === 'example.com') {
    req.targetsite = {
      me: 'http://example.com/',
      endpoint: 'https://tokens.indieauth.com/token',
    };
    next();
  } else {
    res.sendStatus(404);
  }
});

app.use('/micropub/:targetsite', micropub({
  logger: logger,          // a logger object that uses the same API as the bunyan module
  userAgent: 'my-app/1.0', // a user-agent that will be prepended to the module's own user-agent to indicate
                           // to IndieAuth endpoints who it is that makes the verification requests
  tokenReference: function (req) {
    // Find the token reference we added to the request object before and return it
    return req.targetsite;
  },
  // And lastly: Do something with the created micropub document
  handler: function (micropubDocument, req) {
    // Do something with the micropubDocument and return a Promise to communicate status of the handling
    return Promise.resolve().then(function () {
      return { url: 'http://example.com/url/to/new/post' };
    });
  }
}));

// Start the Express server on a port, like port 3000!
app.listen(3000);

Options

  • tokenReferencerequired – either an object with two keys, me and endpoint, or a function that receives the request object and returns an object with those two keys. The me key signify what identity it is that's expected for a succesful authorization and the endpoint key indicates what endpoint the token should be verified with. Can also be or return an array of multiple references.
  • handlerrequired – the function that will be called with the handled micropub document and the request object. It's this functions responsibility to actually act on the received data and do something with it. Should return a Promise resolving to an object with a url key containing the url of the created item to indicate success. If the Promise is rejected or the url key is missing or falsy in the resolved Promise, then a 400 error will be returned to indicate failure.
  • userAgentrecommended – a user-agent string like your-app-name/1.2.3 (http://app.example.com/) that gets prepended to the user-agent of micropub-express itself when verifying received tokens against an endpoint
  • queryHandleroptional – a function that will be called whenever a ?q= query is made to the Micropub endpoint. It's this functions responsibility to execute the query and respond with the relevant data. Should return a Promise resolving to an object containing the query result. Keys on the object should not include any [], those will be added in the encoded result where relevant. If the Promise resolves to something falsy, then a 400 error will be returned to indicate that the query type is unsupported. If the Promise is rejected, then a 400 error will be returned to indicate failure.
  • loggeroptional – a bunyan compatible logger, like bunyan itself or some other module. Defaults to bunyan-duckling which logs with console.log() and console.error()

Format of micropubDocument

The format closely matches the JSON-representation of Micropub.

It contains three top level keys:

  • type – an array containing the type that is that's going to be created. Eg. ['h-entry']
  • properties – an object containing all of the microformat properties of the document as arrays containing strings. Eg. content: ['foobar']
  • mp – an object containing all of the micropub directives as arrays containing string. Eg. 'syndicate-to': ['http://twitter.com/example'] for an 'mp-syndicate-to' directive.
  • files – an object that can contain three keys, audio, video, photo, which in turn contains arrays of objects with a filename and a buffer key with the name and content of the files.

Full example:

{
  type: ['h-entry'],
  properties: {
    content: ['hello world'],
  },
  mp: {
    'syndicate-to': ['http://twitter.com/example'],
  },
  files: {
    photo: [
      {
        filename: 'example.jpg',
        buffer: new Buffer() // A Node.js buffer with the content of the file.
      }
    ]
  }
}

Other useful modules

  • format-microformat – a module that takes a micropubDocument as its input and then formats filenames, URL:s and file content from that data so one gets some standard data which one then can publish elsewhere – like to a Jekyll blog or something.
  • github-publish – a module that takes a filename and content and publishes that to a GitHub repository. A useful place to send the formatted data that comes out of format-microformat if one wants to add it to a GitHub hosted Jekyll blog of some kind, like eg. GitHub Pages.

node-micropub-express's People

Contributors

fossabot avatar renovate-bot avatar renovate[bot] avatar voxpelli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-micropub-express's Issues

Syndication Target Draft Spec Changed

I'm working my way through this, and when I figure it out I will hopefully be able to add a pull request but wanted to share that checking with @aaronpk in IRC, he pointed out Quill matches with the current spec, specifically for syndication-target requiring passing an uid and name.

If I'm deciphering the code correctly, node-micropub-express needs to be able to handle both params in the queryHandler. Am I on the right path?

Media Endpoint Support

I've been trying to get media endpoint support working on my site. I ran into the problem that micropub-express only supports video, photo and audio upload fields. Also the checks that require certain micropub fields don't apply to the media endpoint.

I managed to hack together support, so can create a pull request but it's really not good. I think ideally there would be a mediaHandler option, but I'm not sure how to create it myself.

Cheers,
Grant

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Content property shouldn't always be required

Hi there,

I recently was having issues with posting from https://teacup.p3k.io as it does not send a content property.

And this library requires a value at index.js#L252, I manually got it working by just making it if (!data.properties) but I don't know if that's the best option or not.

Support space-separated list of scopes

I default to using a space-delineated string, which I think is pretty standard but not explicitly required. https://indieauth.com, for example will just pass on whatever you supply in the request, but the auth screen splits scope on a space to show the list of requested scopes.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v2
  • github/codeql-action v1
  • github/codeql-action v1
  • github/codeql-action v1
.github/workflows/lint.yml
  • actions/checkout v2
  • actions/setup-node v2
.github/workflows/nodejs.yml
  • actions/checkout v2
  • actions/setup-node v2
  • coverallsapp/github-action v1.1.2
  • coverallsapp/github-action v1.1.2
npm
package.json
  • body-parser ^1.13.1
  • bunyan-adaptor ^4.0.1
  • express ^4.13.0
  • multer ^1.0.1
  • node-fetch ^1.3.0
  • verror ^1.8.1
  • @hdsydsvenskan/ts-ignore-import ^2.0.0
  • @types/body-parser ^1.19.0
  • @types/chai ^4.2.14
  • @types/chai-as-promised ^7.1.3
  • @types/express ^4.17.11
  • @types/mocha ^8.2.0
  • @types/multer ^1.4.5
  • @types/node-fetch ^1.6.9
  • @types/sinon ^9.0.10
  • @types/sinon-chai ^3.2.5
  • @types/supertest ^2.0.10
  • @types/verror ^1.10.4
  • @voxpelli/eslint-config ^5.0.0
  • chai 4.2.0
  • chai-as-promised 7.1.1
  • dependency-check ^4.1.0
  • eslint ^6.8.0
  • eslint-config-standard ^14.1.1
  • eslint-plugin-es ^3.0.1
  • eslint-plugin-import ^2.22.1
  • eslint-plugin-jsdoc ^21.0.0
  • eslint-plugin-mocha ^6.3.0
  • eslint-plugin-node ^11.1.0
  • eslint-plugin-promise ^4.2.1
  • eslint-plugin-security ^1.4.0
  • eslint-plugin-standard ^4.1.0
  • eslint-plugin-unicorn ^19.0.1
  • husky ^4.3.8
  • installed-check ^3.0.0
  • mocha ^8.2.1
  • nock ^13.0.6
  • npm-run-all ^4.1.5
  • nyc ^15.1.0
  • sinon ^9.2.4
  • sinon-chai ^3.5.0
  • supertest 6.1.3
  • type-fest ^0.20.2
  • typescript ^4.1.3
  • node >=12.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

JSON processing should accept all data

At the moment the processJSONencodedBody function strips out anything from the micropub request that is not in the properties and doesn't start with mp-, and I'm not sure if this is correct.

My use case is I am starting to implement collections which should be sent as an array of children. I'm not 100% sure if children will be the only other property to support or if there will be more in the future but it is a 3 line fix to pass on all unrecognised data in the micropub request: see this commit

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.