1.7.13 • Published 21 days ago

apiker v1.7.13

Weekly downloads
-
License
MIT
Repository
github
Last release
21 days ago

📦 Installation & Usage

With Apiker, you can create an API in only 3 lines of code

import { apiker, res } from "apiker";
const routes = { "/users/:id/hello": () => res("Hello World!") };

apiker.init({ routes, exports, objects: ["Common"] });

➡️ GET /users/my-user/hello

{ "message": "Hello World!" }

Check out the Getting Started page to begin.

Note: To run Apiker, you need a Cloudflare account with Durable Objects access.

⭐ Features

  • 📕 Routing and State management
  • 🔑 Auth, JWT-based (Register, Login, Refresh token, Delete user, Forgot user, Verify user email)
  • ✅ OAuth handlers (GitHub)
  • ⚡️Automatically updates Durable Object migrations, classes and bindings so you don't have to.
  • 🛑 Rate Limiting / Flooding mitigation
  • 🛡️ Firewall support (IP bans with Cloudflare Firewall)
  • 📧 Email support (with Brevo)
  • ⚙️ Simple Admin panel
  • 👤 Geolocation handlers
  • 📝 Logging handlers

📕 Examples

1. Route example

import { res, res_400 } from "apiker";

export const myRouteHandler = async ({
  request, // https://developers.cloudflare.com/workers/runtime-apis/request/
  body, // The body of your request, such as form params or plaintext, depending on request content-type
  headers, // Request headers. Response headers are located at `apiker.responseHeaders`
  matches, // Your query params and route parts
  state // The state method used to interact with permanent storage (check the examples below & docs for more info)
}) => {
  // If we want to allow POST only, we explicitly check for it :
  if(request.method !== "POST"){
     // returns 400 Bad Request error. You can also use `res("Invalid Method", 405)`
     // https://github.com/hodgef/apiker/blob/master/src/components/Response/Response.ts#L10
     return res_400();
  }

  // We'll return the request body passed, for example POST form parameters
  // `res` and `res_000` functions respond with JSON. To respond with raw text you can use `resRaw`
  // https://github.com/hodgef/apiker/blob/master/src/components/Response/Response.ts#L23
  return res({ body });
};
const routes = {
  "/users/myroute": myRouteHandler
};

Discuss: https://github.com/hodgef/apiker/issues/133

2. State: Save value to Common object (shared by all visitors)

import { res } from "apiker";

export const example1_saveValueCommon = async ({ state }) => {
  // Using `state` with no parameters will default to the Common object
  const count = ((await state().get("count")) || 0) + 1;
  await state().put({ count });
  return res({ count });
};

➡️ GET /example1

{ "count": 1 }

View Source | View Demo

3. State: Save value to a different object, and use one object instance per visitor

import { res } from "apiker";

export const example2_saveValueDiffObject = async ({ state }) => {
  const count = (await state("Example2").get("count") || 0) + 1;
  await state("Example2").put({ count });
  return res({ count });
};

// In index.js ...
apiker.init({
 ...
 objectStateMapping: {
    // One object instance per user IP
    Example2: OBMT.SIGNEDIP
  }
});

➡️ GET /example2

{ "count": 1 }

View Source | View Demo

4. State: Use one object instance per route parameter value

import { res } from "apiker";

export const example3_instancePerRouteParam = async ({ state, matches }) => {
  // Get username from route (/example3/:username)
  const username = matches.params.username;
  const acceptedUsernames = ["bob", "rob", "todd"];

  if (acceptedUsernames.includes(username)) {
    const { name = username, count = 0 } = (await state("Example3").get("username")) || {};
    const payload = {
      username: {
        name,
        count: count + 1
      }
    };

    await state("Example3").put(payload);
    return res(payload);
  } else {
    return res({ acceptedUsernames });
  }
};


// In index.js ...
apiker.init({
 ...
 objectStateMapping: {
    // Mapped to the parameter `username` in the route
    Example3: "username"
  }
});

➡️ GET /example3/bob

{
    "username": {
        "name": "bob",
        "count": 1
    }
}

View Source | View Demo

For more details and examples, check out the Documentation.

✅ Contributing

PRs and issues are welcome. Feel free to submit any issues you have at: https://github.com/hodgef/Apiker/issues

Questions? Join the chat

1.7.13

21 days ago

1.7.11

3 months ago

1.7.12

3 months ago

1.7.10

3 months ago

1.7.9

4 months ago

1.7.8

4 months ago

1.7.7

4 months ago

1.7.6

4 months ago

1.7.5

4 months ago

1.7.4

4 months ago

1.7.3

4 months ago

1.7.2

4 months ago

1.7.1

4 months ago

1.7.0

4 months ago

1.6.10

4 months ago

1.6.9

5 months ago

1.6.8

5 months ago

1.6.4

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.30

2 years ago

1.5.32

2 years ago

1.5.31

2 years ago

1.5.34

2 years ago

1.5.33

2 years ago

1.5.36

2 years ago

1.5.35

2 years ago

1.5.38

2 years ago

1.5.37

2 years ago

1.5.39

2 years ago

1.5.40

2 years ago

1.5.29

2 years ago

1.5.28

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.12

2 years ago

1.2.13

2 years ago

1.2.10

2 years ago

1.2.11

2 years ago

1.2.16

2 years ago

1.2.17

2 years ago

1.2.14

2 years ago

1.2.15

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.2.9

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.5.9

2 years ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.2.18

2 years ago

1.2.19

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.5.10

2 years ago

1.2.20

2 years ago

1.5.12

2 years ago

1.5.11

2 years ago

1.5.14

2 years ago

1.2.23

2 years ago

1.5.13

2 years ago

1.5.16

2 years ago

1.2.21

2 years ago

1.5.15

2 years ago

1.2.22

2 years ago

1.5.18

2 years ago

1.5.17

2 years ago

1.5.19

2 years ago

1.5.21

2 years ago

1.5.20

2 years ago

1.5.23

2 years ago

1.5.22

2 years ago

1.5.25

2 years ago

1.5.24

2 years ago

1.5.27

2 years ago

1.5.26

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.51

2 years ago

1.0.49

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.40

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

0.0.1

3 years ago