0.0.34 • Published 2 years ago

dumb-durable-object v0.0.34

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Dumb Durable Object

This is a small package to ease the friction when communicating with a Durable Object binding. This package is under development and if you want to use it in any stable capacity I suggest you copy over the code and maintain it yourself. If you do any good improvements it'd be greatly appreciated if you can contribute back. There will likely be a lot of breaking packages when this package updates as it is in its infancy.

Usage

This is a simple example using the library.

Simple response

// In your worker most likely
class DurableObjectExample extends CallableDurableObject {
  @callable // Decorator that ensures the type signature required for it to be callable
  helloWorld(name: string) {
    return ok(200, `Hello world, ${name}!`);
  }
}

// In whoever is calling the worker, for example a pages function
type Env = {
  DO_EXAMPLE: DurableObjectNamespaceIs<DurableObjectExample>;
};

export async function onRequest({
  request,
  env,
}: {
  request: Request;
  env: Env;
}) {
  const id = "MY_DO_ID"; // Could also be a DurableObjectId
  const c = client(env.DO_EXAMPLE, id);
  const response = await c.helloWorld("MY NAME"); // Absence of error makes the json() function always the successful one

  const value = await response.json()

  return new Response(value, { status: 200 });
}

Error response

// In your worker most likely
class DurableObjectExample extends CallableDurableObject {
  @callable // Decorator that ensures the type signature required for it to be callable
  helloWorld(name: string) {
    if (name === "") {
      return error(422, { message: "Your name was empty!" });
    }
    return ok(200, `Hello world, ${name}!`);
  }
}

// In whoever is calling the worker, for example a pages function
type Env = {
  DO_EXAMPLE: DurableObjectNamespaceIs<DurableObjectExample>;
};

export async function onRequest({
  request,
  env,
}: {
  request: Request;
  env: Env;
}) {
  const id = "MY_DO_ID"; // Could also be a DurableObjectId
  const c = client(env.DO_EXAMPLE, id);
  const response = await c.helloWorld("MY NAME");

  // Since it might return an error we have to disambiguate
  if (!response.ok) {
    const err = await response.json();
    // value is of type { message: string }
    return new Response(err.message, { status: response.status }); // Notice we get the error value here
  }

  const value = await response.json()
  // value is of type string
  return new Response(value, { status: response.status }); // Notice we get the successful value here
}
0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.19

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.33

2 years ago

0.0.34

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.15

2 years ago

0.0.9

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago