Skip to content

Commit

Permalink
export bufbuild helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
gimmyxd committed Dec 9, 2024
1 parent 6d785b3 commit 8387cd2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
6 changes: 2 additions & 4 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ m
Response Messages no longer implement the magic toJSON method, which serializes a message with the Protobuf JSON format when it's passed to `JSON.stringify`. Make sure to always serializes to JSON with the toJson or toJsonString function.

```ts
import { GetObjectsResponseSchema } from '@aserto/aserto-node'
import { toJson } from "@bufbuild/protobuf";
import { GetObjectsResponseSchema, toJson } from '@aserto/aserto-node'

const response = await directoryClient.objects({
objectType: "user",
Expand Down Expand Up @@ -150,8 +149,7 @@ TypeError: Do not know how to serialize a BigInt
This requires [data serialization](#serialization-and-deserialization-of-data):

```ts
import { GetObjectsResponseSchema } from '@aserto/aserto-node'
import { toJson } from "@bufbuild/protobuf";
import { GetObjectsResponseSchema, toJson } from '@aserto/aserto-node'

app.get("/api/users/:id", async (req, res) => {
const id = req.params.id;
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,7 @@ Use [Protocol Buffers](https://github.com/bufbuild/protobuf-es) to serialize dat
```ts
import { GetObjectsResponseSchema } from "@aserto/aserto-node";
import { toJson } from "@bufbuild/protobuf";
import { GetObjectsResponseSchema, toJson } from "@aserto/aserto-node";

const objects = await directoryClient.objects({objectType: "user"});
const json = toJson(GetObjectsResponseSchema, objects)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/authorizer/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { create } from "@bufbuild/protobuf";
import { Code, ConnectError } from "@connectrpc/connect";

import {
create,
decisionTreeOptions,
DecisionTreeResponseSchema,
IsResponseSchema,
Expand Down
4 changes: 2 additions & 2 deletions __tests__/directory/v3/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as fs from "fs";
import { describe } from "node:test";
import { create } from "@bufbuild/protobuf";
import { Code, ConnectError } from "@connectrpc/connect";
import { createAsyncIterable } from "@connectrpc/connect/protocol";
import * as connectNode from "@connectrpc/connect-node";

import {
CheckResponseSchema,
ConfigError,
create,
createImportRequest,
DeleteManifestResponseSchema,
DeleteObjectResponseSchema,
Expand All @@ -29,7 +29,7 @@ import {
SetManifestResponseSchema,
SetObjectResponseSchema,
SetRelationResponseSchema,
} from "../../../lib/index";
} from "../../../lib";
jest.mock("fs");

describe("DirectoryV3", () => {
Expand Down
40 changes: 38 additions & 2 deletions __tests__/integration/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import express, { Express } from "express";
import express, { Express, Request, Response } from "express";
import nJwt from "njwt";
import { describe } from "node:test";
import request from "supertest";
import { toJson } from "@bufbuild/protobuf";

import {
AnonymousIdentityMapper,
Expand All @@ -20,6 +19,7 @@ import {
policyContext,
policyInstance,
readAsyncIterable,
toJson,
} from "../../lib";
import { Topaz, TOPAZ_TIMEOUT } from "../topaz";

Expand All @@ -43,6 +43,42 @@ describe("Integration", () => {
await topaz.stop();
});

describe("express", () => {
const app = express();
app.use(express.json());

it("serializes response to json", async () => {
const ListUsers = async (_req: Request, res: Response) => {
try {
const users = await directoryClient.objects({ objectType: "user" });
const usersJson = toJson(GetObjectsResponseSchema, users);

res.status(200).send(usersJson);
} catch (error) {
console.error(error);
res.status(500).send(error);
}
};
app.get("/users", ListUsers);

const res = await request(app)
.get("/users")
.set("Content-type", "application/json");
expect(res.status).toBe(200);
expect(res.body).toEqual(
expect.objectContaining({
results: expect.arrayContaining([
expect.objectContaining({ id: "[email protected]" }),
expect.objectContaining({ id: "[email protected]" }),
expect.objectContaining({ id: "[email protected]" }),
expect.objectContaining({ id: "[email protected]" }),
expect.objectContaining({ id: "[email protected]" }),
]),
}),
);
});
});

describe("Directory Reader", () => {
it("fallsback to reader proxy when reader is not configured", async () => {
const readerClient = DirectoryServiceV3({
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ export {
export * from "./errors";
export * from "./authorizer/types";
export * from "./directory/v3/types";
export * from "@bufbuild/protobuf";

0 comments on commit 8387cd2

Please sign in to comment.