Api enchantment

For example twitter api:

// twitter-api.js
import api from 'bivrost/http/api';

export default (authToken, config = {}) => {
  const adapter = fetchAdapter();

  let twitterApi = api({
    protocol: 'https:',
    host: 'api.twitter.com'
    prefix: '1.1',
    ...config,
    adapter: fetchAdapter({
      headers: {
        ...config.headers,
        TwitterOAuth: authToken
      }
    });
  });

  // Returns the 20 most recent mentions (tweets containing a users’s @screen_name)
  // for the authenticating user.
  twitterApi.mentionsTimeline = apiInstance('GET /statuses/mentions_timeline');

  // Returns a single Tweet, specified by the id parameter.
  twitterApi.show = apiInstance('GET /statuses/show/:id');

  return twitterApi;
}

Usage:

import createTwitterApi from 'twitter-api';

const twitterApi = createTwitterApi(AUTH_TOKEN);

// manual call
twitterApi('GET /statuses/mentions_timeline');

// shortcut call
twitterApi.mentionsTimeline();

twitterApi.show({
  id: TWEET_ID,
});

Same could be implemented for

# Group api by backend services

Especially it is very useful for microservice architecture.

  • src/data/api/auth.js - api for auth service service
  • src/data/api/users.js - api for application users service
  • src/data/api/notifications.js - api for notification service

Also such api functions could be easily shared between other applications.

results matching ""

    No results matching ""