0.0.34 • Published 1 year ago

dumb-durable-object v0.0.34

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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

1 year ago

0.0.21

1 year ago

0.0.22

1 year ago

0.0.23

1 year ago

0.0.24

1 year ago

0.0.25

1 year ago

0.0.19

1 year ago

0.0.30

1 year ago

0.0.31

1 year ago

0.0.32

1 year ago

0.0.33

1 year ago

0.0.34

1 year ago

0.0.26

1 year ago

0.0.27

1 year ago

0.0.28

1 year ago

0.0.29

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.15

1 year ago

0.0.9

1 year ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago