Giter Club home page Giter Club logo

ts-agent's Introduction

hippiemedia/agent

TL;DR

import factory from '@hippiemedia/agent';

agent = factory();
let resource = await agent.follow('http://haltalk.herokuapp.com/');

What ?

An hypermedia agent capable of discovering and navigating a web of linked resources.

How ?

npm install @hippiemedia/agent

build webpack bundle

npm install
npm run build

Then you can create an agent. It's like your browser, but it understands more hypermedia formats:

import factory from '@hippiemedia/agent';

agent = factory();
  • html [ ]
  • ld+json (+hydra) [ ]
  • siren+json [ ]
  • hal+json [x]
  • hal-forms [x]
  • vnd.api+json [ ]
  • collection+json [ ]
  • uber+json [ ]
  • your own adapter...

This agent is capable of fetching a resource, parsing its content based on its content type,
and populating a Resource object with operations, links and queries.

Discoverability

It's important to let the agent discover links instead of hardcoding them.
You can do that by introspecting resources:

let menu = resource.links.map(link => render(`<a href="${link.href}">${link.rel}</a>`));

let actions = resource.operations.map(o => render(`<form method="${o.method}" action="${o.href}">
    ${o.fields.map(field => render(`<input type=${field.type} />`))}
</form>`));

Operations

You can apply some operations on a resource, like POSTing a new resource to a collection, or DELETE a resource.

let resource = await agent.follow('http://some-hypermedia-api.com/resource1');
let newResource = await resource.follow('comment').then(r => {
    r.operation('default').fill({
        author: 'me',
        text: 'fuck yeah'
    }).submit()
});

Links

You can follow links as you follow them in your browser.

let resource = await agent.follow('http://some-hypermedia-api.com/resource1');
console.log(resource.links);
let comments = await resource.followAll('comments');

Templated Links

You can follow templated links as you follow links:

let resource = await agent.follow('http://some-hypermedia-api.com/resource1');
let comments = await resource.follow('user', {id: '1'});

Why ?

The current state of http APIs sucks. Everybody makes its own shit in its own corner
without embracing what made the web so successful (hateoas, links, auto-discovery, ...)

json-ld + hydra is the future, yet everybody tries to replace poor REST with graphql,
thinking that graphql replaces REST (while it's only complementary at best),
thinking that client-side-hardcoded-server-structure is better than auto-discovered, self-documented, dumb[1] clients,
thinking that json is better that xml,
thinking that raw-data structures worth anything without its semantics,
thinking that hardcoding URLs client-side and API versioning is better than letting the server do the job.
/rant

[1] dumb clients are actually smart: they know as much about your API than firefox knows about just another mother fucking website.
Yet this very same browser is capable of displaying it and any other website. Now who's smart?

ts-agent's People

Contributors

docteurklein avatar olivierpontier avatar rouliane avatar tamarasaurus avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

tamarasaurus

ts-agent's Issues

provide a fallback mechanism to handle unknown types

agent.js:68 Uncaught (in promise) Error: application/json
    at agent.js:68
    at Agent../node_modules/@hippiemedia/agent/dist/src/agent.js.Agent.getAdapter (agent.js:68)
    at Agent../node_modules/@hippiemedia/agent/dist/src/agent.js.Agent.build (agent.js:62)
    at Agent.<anonymous> (agent.js:55)
    at step (agent.js:32)
    at Object.next (agent.js:13)
    at fulfilled (agent.js:4)

the agent should handle the absence of content-type header


hal-json.js:17 Uncaught (in promise) TypeError: Cannot read property 'includes' of null
    at HalJson../node_modules/@hippiemedia/agent/dist/src/adapter/hal-json.js.HalJson.supports (hal-json.js:17)
    at agent.js:67
    at Array.find (<anonymous>)
    at Agent../node_modules/@hippiemedia/agent/dist/src/agent.js.Agent.getAdapter (agent.js:66)
    at Agent../node_modules/@hippiemedia/agent/dist/src/agent.js.Agent.build (agent.js:62)
    at Agent.<anonymous> (agent.js:55)
    at step (agent.js:32)
    at Object.next (agent.js:13)
    at fulfilled (agent.js:4)

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.