1.1.12 • Published 5 months ago

proah v1.1.12

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

Proah কি?

এটি একটি ছোট্টো লাইব্রেরি। যেটা HTTP Request পাঠানো এবং Resposne প্রক্রিয়াকরণ কে কিছুটা সহজ করে দেয়। যেটা Proah তার ব্যক্তিগত কাজের জন্য বানিয়েছে। তবে এটা উন্মুক্ত চাইলে যে কেউ ব্যবহার করতে পারে।

🚀 Proah - Lightweight & Customizable HTTP Client

FeatureDescription
🌟 Lightweightখুবই হলকা একটি library
Fast & Simpleসহজেই ব্যবহার যোগ্য
🔄 Auto ParsingJSON, text, blobs, স্বয়ংক্রিয় ভাবে পার্স করা
🎯 Customizableইচ্ছে মতো পরিবর্তন ও সংযোজন যোগ্য
🔥 Modern Fetch-Basedএটি আধুনিক fetch API এর উপর ভিত্তিতে তৈরি
🛠 Supports Extra Requestsproah.extra() ইন্সট্যান্স মেথড পরিপুর্ণ নিয়ন্ত্রণের জন্য
Timeout Supportglobal অথবা নির্দিষ্ট ইন্সট্যান্স মেথড এ timeout যুক্ত করা

যেভাবে ব্যবহার করা যাবে,

Installation / ইন্সটেলেশন:

npm install proah  # Or yarn add proah

Includes in Project

/*For ESM*/
import Proah from "proah";

/*For CJS*/
const Proah = require("proah");

Proah instance তৈরিঃ

const proah = new Proah({
  baseURL: "https://proah-post.vercel.app",
  timeout: 12000,
  methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
  mode: "cors",
  cache: "force-cache",
  credentials: "include",
  // resultProps: "myData",
});

Proah Config Props :

PropertyDescriptionDefaultOptional
methodsmethods Array-র মধ্যে যুক্ত করুন, যেসব HTTP Request method ব্যবহার করবেন[]No
baseURLAPI রিকুয়েস্ট এর জন্য baseURLnothingYES
resultPropsএখানে ডিফাইন্ড করা যাবে, চূড়ান্ত Response Data যেখানে প্রাপ্ত হবেdataYES
credentialsএখানে credentials value যুক্ত করা যাবে HTTP Request এর জন্যomitYES
cacheএখানে cache value যুক্ত করা যাবে HTTP Request এর জন্যdefaultYES
modeএখানে mode value যুক্ত করা যাবে HTTP Request এর জন্যcorsYES
timeoutএখানে timeout(মিলি সেকেন্ড) value যুক্ত করা যাবে HTTP Request এর জন্য,5000YES

Proah instance methods (6)

  1. proah.get()
    Syntax: proah.get('/path-to-endpoint', { Request options })
  2. proah.post()
    Syntax: proah.post('/path-to-endpoint', { Request options })
  3. proah.put()
    Syntax: proah.put('/path-to-endpoint', { Request options })
  4. proah.patch()
    Syntax: proah.patch('/path-to-endpoint', { Request options })
  5. proah.delete()
    Syntax: proah.delete('/path-to-endpoint', { Request options })
  6. proah.extra()
    Syntax: proah.extra('/path-to-endpoint', { Request options })


    Request options properties:

    PropertyDefaultOptionalReplaceable
    bodyকিছুই নাহ্যাঁহ্যাঁ
    queryকিছুই নাহ্যাঁহ্যাঁ
    headers{'Content-Type':'application/json', ...}হ্যাঁহ্যাঁ
    timeoutProah কনফিগারেশন থেকে নেওয়া হবে, অন্যথায় ডিফল্টহ্যাঁহ্যাঁ
    baseURLProah কনফিগারেশন থেকে নেওয়া হবে, অন্যথায় কিছুই নাহ্যাঁহ্যাঁ
    credentialsProah কনফিগারেশন থেকে নেওয়া হবে, অন্যথায় ডিফল্টহ্যাঁহ্যাঁ
    modeProah কনফিগারেশন থেকে নেওয়া হবে, অন্যথায় ডিফল্টহ্যাঁহ্যাঁ
    cacheProah কনফিগারেশন থেকে নেওয়া হবে, অন্যথায় ডিফল্টহ্যাঁহ্যাঁ
    resultPropsProah কনফিগারেশন থেকে নেওয়া হবে, অন্যথায় ডিফল্টহ্যাঁহ্যাঁ
    methodProah লাইব্রেরির থেকে নেওয়া হবেহ্যাঁনা

    and more....

Example Request

Create a Basic Instance

const proah = new Proah({
  baseURL: "https://proah-post.vercel.app",
  methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
  timeout: 12000,
});

1. GET

Example Success Response handling:

const { data, error, status, statusText } = await proah.get("/api/posts", {
  timeout: 8000,
});

if (data) {
  console.log(data);
} else {
  console.log(error);
}
// Console Output
{
  success: true,
  status: 200,
  message: 'All Posts here',
  data: [
    {
      userId: 1,
      id: 1,
      title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
      body: 'quia et suscipit\n' +
        'suscipit recusandae consequuntur expedita et cum\n' +
        'reprehenderit molestiae ut ut quas totam\n' +
        'nostrum rerum est autem sunt rem eveniet architecto'
    },
    {
      userId: 1,
      id: 2,
      title: 'qui est esse',
      body: 'est rerum tempore vitae\n' +
        'sequi sint nihil reprehenderit dolor beatae ea dolores neque\n' +
        'fugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\n' +
        'qui aperiam non debitis possimus qui neque nisi nulla'
    },
    ...more
  ]
}

Example Failed Response handling:

const { data, error, status, statusText } = await proah.get("/api/postss"); // Invalid endpoint

if (data) {
  console.log(data);
} else {
  console.log(error); // Error output in the console
}
// Console Output
{
  success: false,
  status: 404,
  message: 'Requested data will not be found'
}

2. POST

const post = {
  title: "তবে গল্পটা যদি আরও কিছুটা দুরে যেতো",
  body: "মনে আছে? একদিন আমরা আঁকাশের তারা গুনছিলাম। তুমি বোকার মত বলেছিলে হ্যাঁ। আমি তোমাকে একটা খোঁচা দিলাম। অমনি তোমার হুঁশ ফিরলো আর বললে, ধুর দিনের বেলা কে তাঁরা গুনলো?",
  userId: 1,
};

const { data, error, status, statusText } = await proah.post("/api/posts", {
  body: JSON.stringify(post),
});

if (data) {
  console.log(data);
} else {
  console.log(error); // No Error
}
// Console Output
{
  success: true,
  status: 201,
  message: 'Post Successfully created',
  data: {
    title: 'তবে গল্পটা যদি আরও কিছুটা দুরে যেতো',
    body: 'মনে আছে? একদিন আমরা আঁকাশের তারা গুনছিলাম। তুমি বোকার মত বলেছিলে হ্যাঁ। আমি তোমাকে একটা খোঁচা দিলাম। অমনি তোমার হুঁশ ফিরলো আর বললে, ধুর দিনের বেলা কে তাঁরা গুনলো?',
    userId: 1,
    id: 101
  }
}

3. PUT

const { data, error, status, statusText } = await proah.put("/api/posts/1", {
  body: JSON.stringify({
    title: "foo",
    body: "bar",
    userId: 1,
  }),
});

if (data) {
  console.log(data);
} else {
  console.log(error); // No Error
}
// Console Output
{
  success: true,
  status: 202,
  message: 'Post updated successfully',
  data: { title: 'foo', body: 'bar', userId: 1, id: 1 }
}

4. PATCH

const { data, error, status, statusText } = await proah.patch("/api/posts/1", {
  body: JSON.stringify({
    title: "তুমি আমায় ডেকেছিলে এক মেঘে ঢাকা দিনে।",
  }),
});

if (data) {
  console.log(data);
} else {
  console.log(error); // No Error
}
// Console Output
{
  success: true,
  status: 202,
  message: 'Post partially updated!',
  data: {
    userId: 1,
    id: 1,
    title: 'তুমি আমায় ডেকেছিলে এক মেঘে ঢাকা দিনে।',
    body: 'quia et suscipit\n' +
      'suscipit recusandae consequuntur expedita et cum\n' +
      'reprehenderit molestiae ut ut quas totam\n' +
      'nostrum rerum est autem sunt rem eveniet architecto'
  }
}

5. DELETE

const { data, error, status, statusText } = await proah.delete("/api/posts/5");

if (data) {
  console.log(data);
} else {
  console.log(error); // No Error
}
// Console Output
{ success: true, status: 200, message: 'Post deleted id: 5' }

এই mehod এর মাধ্যমে যেকোনো HTTP Request handle করা যাবে। এবং এটা Developer কে আরও বেশি নিয়ন্ত্রণ দেয়। তবে extra method টা Promise Response Returns করে, সুতরাং এটি ব্যবহার করলে Promise Response টাকে নীজ দায়িত্বে handle করতে হবে।

উল্লেখ্য extra method এর মাধ্যমে HEAD, OPTIONS, এই রিকুয়েস্ট গুলোও ব্যবহার করা যাবে।

GET request with extra method:

const response = await proah.extra("/posts", {
  method: "GET",
  query: {
    _limit: 1,
    _start: 2,
  },
});

const { status, statusText, ok /*... and more*/ } = response;

if (!ok) {
  // Some Error handling Logic
} else {
  // manipulate respone data using response method e.g. response.text(),response.json()
}

একই ভাবে অন্যান্য রিকুয়েস্ট মেথড গুলো প্রয়োগ করা যাবে এই extra মেথড এর মাধ্যমে

যেসব proah instance method ব্যবহার করবেন। সেটার অনুরূপ HTTP method Proah Confguaration এ যুক্ত করতে হবে। যেমনঃ

আপনি যদি proah.get() মেথড ব্যবহার করতে চান তবে, Proah Configuration সে সেটা যুক্ত করতে হবে

const proah = new Proah({
  methods: ["GET"],
  ...others,
});

Proah config এ "GET" methods না যুক্ত করেই যদি proah.get() method call করেন তবেই নিম্নক্ত Error টি দেখতে পারবেন।

npm.io

একই ভাবে যেসব HTTP Request মেথড ব্যবহার করতে ইচ্ছুক সেগুলো Proah Configuration methods Array তে যুক্ত করতে হবে।


আপনি যদি ভুলবসত HTTP Request এর method পরিবর্তন করেন তবে নিম্নক্ত Error টি দেখতে পারবেন।
যেমনঃ

const result = await proah.get("/posts", {
  method: "POST",
  ...others,
});

npm.io

নোটঃ proah.get(), proah.post(), proah.put(), proah.patch(), proah.delete() এই পাঁচটি instance method এর Request method স্পর্শ করা অর্থহীন।


আপনি যদি proah.extra() মেথড ব্যবহার করেন তবে options এর মধ্যে অবশ্যই Request Method যুক্ত করতে হবে। যেমন:

const response = await proah.extra("/posts", {
  method: "GET",
  ...others,
});

const { status, statusText, ok /*... and more*/ } = response;

if (!ok) {
  // Some Error handling Logic
} else {
  // manipulate respone data using response method e.g. response.text(),response.json()
}

আপনি যদি এটা না করেন তবে নিম্নক্ত Error টি দেখতে পারবেন: যেমন

const response = await proah.extra("/posts", {
  ...othes,
});

npm.io


resultProps in Proah Config:

আপনি চাইলে চূড়ান্ত ফলাফলে কোন প্রোপারটিস এর মধ্যে রেসপন্স এর ডেটা আসবে এটা resultProps এর মাধ্যে বলে দিতে পারবেন। by default এটির ভালু "data" থাকে। যেমন এক্ষেত্রে default value "data" কে "myData" দিয়ে পুনর্স্থাপন করা হলো। এখন চূড়ান্ত ফলাফলের "myData" এর মধ্যে সার্ভার থেকে গ্রহণকৃত ডেটা প্রাপ্ত হবে।

const proah = new Proah({
  baseURL: "https://proah-post.vercel.app",
  methods: ["GET"],
  resultProps: "myData", // ⚠️ এটা না করাই ভালো!
});
const { myData, error } = await proah.get("/api/posts", {
  query: {
    _limit: 1,
  },
});

if (myData) {
  console.log(myData);
} else {
  console.log(error);
}
{
  success: true,
  status: 200,
  message: 'All Posts here',
  data: [
    {
      userId: 1,
      id: 1,
      title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
      body: 'quia et suscipit\n' +
        'suscipit recusandae consequuntur expedita et cum\n' +
        'reprehenderit molestiae ut ut quas totam\n' +
        'nostrum rerum est autem sunt rem eveniet architecto'
    }
  ]
}

Bodyless HTTP request for Proah:

 GET HEAD OPTIONS DELETE

উল্লেখিত Bodyless HTTP request গুলোর জন্য option এ যদি body property যুক্ত করা হয় তবে। নিম্নক্ত Error লক্ষ করতে পারবেন। যেমনঃ

(async () => {
  try {
    const response = await proah.extra("/posts/1", {
      method: "HEAD",
      body: JSON.stringify({ name: "Proah", age: undefined }),
    });
    if (response.ok) {
      console.log(await response.headers);
    }
  } catch (error) {
    console.log(error);
  }
})();

npm.io

সাধারণ HTML এ যেভাবে ব্যবহার করার যাবে

app.js

// import proah library
import Proah from "https://www.unpkg.com/proah@1.1.11/dist/index.mjs";

// create proah instance
const proah = new Proah({
  methods: ["GET"],
  baseURL: "https://proah-post.vercel.app/",
  timeout: 10000,
});

// use intance method
proah
  .get("/api/posts", {
    query: {
      _limit: 3,
      _start: 10,
    },
  })
  .then(console.log)
  .catch(console.log);

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- linkup app.js in html -->
    <script type="module" src="./app.js"></script>
  </body>
</html>

Inside browser console

npm.io

Supported HTTP Methods for Proah

GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS ✔️


1.0.2

5 months ago

1.1.0-experiment.1

5 months ago

1.1.9

5 months ago

1.1.0-experiment.6

5 months ago

1.1.8

5 months ago

1.1.0-experiment.7

5 months ago

1.1.0-experiment.8

5 months ago

1.1.7

5 months ago

1.1.0-experiment.9

5 months ago

1.1.6

5 months ago

1.1.0-experiment.2

5 months ago

1.1.5

5 months ago

1.1.0-experiment.3

5 months ago

1.1.0-experiment.4

5 months ago

1.0.4

5 months ago

1.1.0-experiment.5

5 months ago

1.0.3

5 months ago

1.1.12

5 months ago

1.1.11

5 months ago

1.1.0-rc.17

5 months ago

1.1.0-rc.15

5 months ago

1.1.0-rc.7

5 months ago

1.1.0-rc.16

5 months ago

1.1.0-rc.6

5 months ago

1.1.0-rc.13

5 months ago

1.1.0-rc.9

5 months ago

1.1.0-rc.14

5 months ago

1.1.0-rc.8

5 months ago

1.1.0-rc.11

5 months ago

1.1.0-rc.12

5 months ago

1.1.0-rc.10

5 months ago

1.1.0-rc.3

5 months ago

1.1.0-rc.2

5 months ago

1.1.0-rc.5

5 months ago

1.1.0-rc.4

5 months ago

1.1.0-rc.1

5 months ago

1.0.1

1 year ago

1.0.0

1 year ago