3.18.0 • Published 1 month ago

@differentialhq/core v3.18.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

Typescript SDK

This is the official Differential SDK for Typescript.

Installation

npm

npm install @differentialhq/core

yarn

yarn add @differentialhq/core

pnpm

pnpm add @differentialhq/core

Quick Start

1. Initializing Differential

Create a file named d.ts which will be used to initialize Differential. This file will export the Differential instance.

// d.ts

import { Differential } from "@differentialhq/core";

// Initialize Differential with your API secret.
// Get yours at https://console.differential.dev.
export const d = new Differential("YOUR_API_SECRET");

2. Hello World Service

In a separate file, create the "Hello World" service. This file will import the Differential instance from d.ts and define the service.

// service.ts

import { d } from "./d";

// Define a simple function that returns "Hello, World!"
const sayHello = async (to: string) => {
  return `Hello, ${to}!`;
};

// ...and as many other functions as you want, any async function can be a service operation
const callEndpoint = async () => {
  return fetch("https://api.example.com");
};

// Register the function as a service
export const helloWorldService = d.service({
  name: "helloWorld",
  functions: {
    sayHello,
    callEndpoint,
  },
});

3. Calling the Service

When calling the service, use the typeof generic to ensure type safety. This can be done in any file where you need to call the service, like a test file or another service file.

// service-consumer.ts

import { d } from "./d";
import type { helloWorldService } from "./service";

const client = d.client<typeof helloWorldService>("helloWorld");

async function test() {
  const greeting = await client.sayHello("World");
  console.log(greeting); // Outputs: Hello, World!
}

test();

4. Running the Service

To run the service, simply run the file with the service definition. This will start the service and make it available to other services.

tsx service.ts

and then you can invoke the service from another file:

tsx service-consumer.ts

Documentation

Examples

  • Monolith contains an example of a monolith application broken into multiple services.
  • End to end encryption contains an example of how to use Differential's end to end encryption.
  • Caching contains an example of how to use Differential's distributed caching.
3.18.0

1 month ago

3.17.0

1 month ago

3.15.0

2 months ago

3.16.0

2 months ago

3.14.0

2 months ago

3.13.1

3 months ago

3.12.2

3 months ago

3.12.1

3 months ago

3.12.0

3 months ago

3.11.0

3 months ago

3.10.1

3 months ago

3.10.0

3 months ago

3.9.0

3 months ago

3.8.0

4 months ago

3.7.1

4 months ago

3.7.0

4 months ago

3.6.1

4 months ago

3.6.0

4 months ago

3.5.0

4 months ago

3.4.6

4 months ago

3.4.0

4 months ago

3.4.4

4 months ago

3.4.3

4 months ago

3.4.2

4 months ago

3.4.1

4 months ago

3.4.5

4 months ago

3.3.1

4 months ago

3.2.2

4 months ago

3.3.0

4 months ago

3.2.3

4 months ago

3.2.1

4 months ago

3.1.4

4 months ago

3.2.0

5 months ago

3.1.0

5 months ago

3.0.6

5 months ago

3.0.4

5 months ago

3.0.3

5 months ago

3.0.2

5 months ago

3.0.1

5 months ago

3.0.0

5 months ago