1.0.1 • Published 19 hours ago

osnmarket-abstract-core v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
19 hours ago

Abstract Core ✨

Getting started 🏁

What is this 🤷‍♂️?

Abstract core is a library made for Next.js Abstract allowing it to handle everything regarding http request caching and security issues.

HTTP Utilities 🌐

In here you'll have 3 http clients:

  • fetcher
  • useFetch
  • serverFetch

Each of them is based on axios meaning one can use it like one would use axios. They are made to handle the following

fetcher

The fetcher is made to handles all runtime client requests. It will send request on abstractHandler and safe communication is made available between them

Basic usage

import { fetcher } from 'osnmarket-abstract-core/client'

fetcher({
  uri: 'https://example.com',
  options: {
    method: 'POST',
    headers: {
      'content-type': 'application/json'
    }
  },
  body: {
    key: 'any',
    value: 'payload'
  },
  target: 'backend', // if not specified it will use the default target
})

This represents a basic implementation of the fetcher. Also if one wanted to use special axios methods, one can do this:

import { fetcher } from 'osnmarket-abstract-core/client'

fetcher({
  uri: 'https://example.com/form/1',
  options: {
    axs_method: 'getForm'
  }
  // The rest ...
})

Addons

You'll find special args you can use in the fetcher

  • encrypt It's an array which once specified, will encrypt those keys inside the response before it's returned to the client; even if the specified keys are an array or an object.
fetcher({
  uri: 'https://example.com/user/1',
  encrypt: ['id', 'uuid', 'customer']
})

// Which response should be this

{
  "id": 1,
  "uuid": "12Ddzad-dazdza-deaeza-212ez",
  "customer": {
    "name": "Doe",
    "surname": "John",
    "adress": "Townville"
  },
  "status": "active"
}

// Would be

{
  "id": "aazoezaezaezaea", // Random gibberish
  "uuid": "aazoezaezaezaea", // Random gibberish
  "customer": "zaeazeazeazeazed", // Random gibberish
  "status": "active"
}

When you do that one might ask "Wait 🤔, that's cold 🥶 but how to I consume it on my code!?"

And you're right Meum 🤗, How?

Use secureContentDecryption 🥳

import { fetcher } from 'osnmarket-abstract-core/client'
import { secureContentDecryption } from 'osnmarket-abstract-core/utils'

const { data } = await fetcher({
  uri: 'https://example.com/user/1',
  encrypt: ['id', 'uuid', 'customer']
})

console.log({
  ...data,
  "customer": JSON.parse(secureContentDecryption(data.customer))
})

// JSON.parse Because objects and arrays are stringified

// Would give

{
  "id": "aazoezaezaezaea", // Random gibberish
  "uuid": "aazoezaezaezaea", // Random gibberish
  "status": "active",
  "customer": {
    "name": "Doe",
    "surname": "John",
    "adress": "Townville"
  }
}
  • user_token

Usually, fetcher will use the backend's token (automatic authentication) but when for example you have an authenticated user, you can specify the token of the user to override the default token.

import { fetcher } from 'osnmarket-abstract-core/client'
import { secureContentDecryption } from 'osnmarket-abstract-core/utils'

const { data } = await fetcher({
  uri: 'https://example.com/user/1',
  user_token: 'Bearer token'
})

useFetch

Behind this hook is SWR so use it in the same way; knowing it uses as http client the fetcher so what ever is implemented on it is available on it. (Yep Eli it saves me a lot of explanation 🌚)

import { useFetch } from 'osnmarket-abstract-core/hook'

const { data } = await useFetch({
  uri: 'https://example.com/user/1'
})

Addons

  • refreshInterval

Sends request periodically

import { useFetch } from 'osnmarket-abstract-core/hook'

const { data } = await useFetch({
  uri: 'https://example.com/user/1',
  refreshInterval: 5000 // each five seconds
})

serverFetch

It's the only utility that won't use the abstractHandler because it's usable only at server level but it works the same way fetcher does (Saves me a lot of explanation too 😅)

import { serverFetch } from 'osnmarket-abstract-core/server'

const { data } = await serverFetch({
  uri: 'https://example.com/user/1'
})
1.0.1

19 hours ago

1.0.0

19 hours ago

0.5.2

21 days ago

0.5.1

21 days ago

0.4.9

23 days ago

0.4.8

23 days ago

0.4.5

23 days ago

0.4.4

23 days ago

0.4.7

23 days ago

0.4.6

23 days ago

0.5.0

23 days ago

0.4.3

23 days ago

0.3.9

24 days ago

0.3.8

24 days ago

0.4.1

24 days ago

0.4.0

24 days ago

0.4.2

24 days ago

0.3.7

24 days ago

0.3.6

2 months ago

0.3.5

2 months ago