0.0.11 • Published 5 years ago

soni v0.0.11

Weekly downloads
11
License
MIT
Repository
-
Last release
5 years ago

Strategy Oriented Network Interface

https://www.npmjs.com/package/soni

https://davidwells.io/blog/publishing-flat-npm-packages-for-easier-import-paths-smaller-consumer-bundle-sizes/

https://glebbahmutov.com/blog/subfolders-as-dependencies/

  • Does not use cookies anymore, change terminology in options *

Examples

Get products from TCG.Codes after fetching a session token. Once this session token is fetched, store it in cookies and bind it to the headers of outgoing requests.

import React, { useState } from 'react';
import { get } from 'soni/http';
import { http } from 'soni/strategy';

const api = http({ 
  domain: 'https://api.tcg.codes',
  cookies: ['session'],
  headers: { session: 'token.session' },
  strategy: {
    validate: store => {
      const tokens = store.fetch();
      const valid = Boolean(tokens && tokens.session) && tokens.session.length > 0;
      return valid;
    },
    process: async (store) => {
      store.flush();
      const { json } = await get({ uri: 'https://api.tcg.codes/auth/tokens' });
      if (!json.session)
        throw new Error('Missing expected property `session` in API response');
      store.mutate(json, false);
      store.cookify();
    }
}});

export default () => {
  const intialProducts: any = [];
  let [products, setProducts] = useState(intialProducts);
  (async () => {
    if (products.length) return;
    const res = await api.get('/products/codes');
    if (res.status === 200)
      setProducts(res.json);
  })();
  return (
    <main>
      Products: { products.map((p:any,i:number) => <div key={p.ProductId}>{p.Name}</div>) }
    </main>
  );
};
0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago