Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ supported.

### Install

#### npm
#### Node.js

```bash
npm install @upstash/redis
Expand Down
66 changes: 66 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/nextjs/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export type HeaderProps = {

export default function Header({ breadcrumbOptions }: HeaderProps) {
return (
<header className="relative z-10 flex items-center bg-gray-900 py-4 px-6 text-gray-50">
<header className="relative z-10 flex items-center px-6 py-4 bg-gray-900 text-gray-50">
<Breadcrumb {...breadcrumbOptions} />
<div className="ml-auto hidden sm:block">
<div className="hidden ml-auto sm:block">
<StarButton {...[...breadcrumbOptions?.data].pop()} />
</div>
</header>
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/components/ReadBlogPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export default function ReadBlogPost({
}: {
children: React.ReactNode;
}) {
return <div className="bg-gray-100 p-4 text-center">{children}</div>;
return <div className="p-4 text-center bg-gray-100">{children}</div>;
}
4 changes: 2 additions & 2 deletions examples/nextjs_edge/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export type HeaderProps = {

export default function Header({ breadcrumbOptions }: HeaderProps) {
return (
<header className="relative z-10 flex items-center bg-gray-900 py-4 px-6 text-gray-50">
<header className="relative z-10 flex items-center px-6 py-4 bg-gray-900 text-gray-50">
<Breadcrumb {...breadcrumbOptions} />
<div className="ml-auto hidden sm:block">
<div className="hidden ml-auto sm:block">
<StarButton {...[...breadcrumbOptions?.data].pop()} />
</div>
</header>
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs_edge/components/ReadBlogPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export default function ReadBlogPost({
}: {
children: React.ReactNode;
}) {
return <div className="bg-gray-100 p-4 text-center">{children}</div>;
return <div className="p-4 text-center bg-gray-100">{children}</div>;
}
4 changes: 2 additions & 2 deletions examples/nextjs_export/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export type HeaderProps = {

export default function Header({ breadcrumbOptions }: HeaderProps) {
return (
<header className="relative z-10 flex items-center bg-gray-900 py-4 px-6 text-gray-50">
<header className="relative z-10 flex items-center px-6 py-4 bg-gray-900 text-gray-50">
<Breadcrumb {...breadcrumbOptions} />
<div className="ml-auto hidden sm:block">
<div className="hidden ml-auto sm:block">
<StarButton {...[...breadcrumbOptions?.data].pop()} />
</div>
</header>
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs_export/components/ReadBlogPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export default function ReadBlogPost({
}: {
children: React.ReactNode;
}) {
return <div className="bg-gray-100 p-4 text-center">{children}</div>;
return <div className="p-4 text-center bg-gray-100">{children}</div>;
}
23 changes: 16 additions & 7 deletions pkg/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import { UpstashError } from "../error.ts";
import { Requester } from "../http.ts";
import { parseResponse } from "../util.ts";

type Serialize = (data: unknown) => string;
type Serialize = (data: unknown) => string | number | boolean;
type Deserialize<TResult, TData> = (result: TResult) => TData;

const defaultSerializer: Serialize = (c: unknown) =>
typeof c === "string" ? c : JSON.stringify(c);
const defaultSerializer: Serialize = (c: unknown) => {
switch (typeof c) {
case "string":
case "number":
case "boolean":
return c;

default:
return JSON.stringify(c);
}
};

export type CommandOptions<TResult, TData> = {
/**
Expand All @@ -27,7 +36,7 @@ export type CommandOptions<TResult, TData> = {
* TResult is the raw data returned from upstash, which may need to be transformed or parsed.
*/
export class Command<TResult, TData> {
public readonly command: string[];
public readonly command: (string | number | boolean)[];
public readonly serialize: Serialize;
public readonly deserialize: Deserialize<TResult, TData>;
/**
Expand All @@ -36,7 +45,7 @@ export class Command<TResult, TData> {
* You can define a custom `deserialize` function. By default we try to deserialize as json.
*/
constructor(
command: (string | unknown)[],
command: (string | boolean | number | unknown)[],
opts?: CommandOptions<TResult, TData>,
) {
this.serialize = defaultSerializer;
Expand All @@ -45,7 +54,7 @@ export class Command<TResult, TData> {
? opts?.deserialize ?? parseResponse
: (x) => x as unknown as TData;

this.command = command.map(this.serialize);
this.command = command.map((c) => this.serialize(c));
}

/**
Expand All @@ -59,7 +68,7 @@ export class Command<TResult, TData> {
throw new UpstashError(error);
}
if (typeof result === "undefined") {
throw new Error(`Request did not return a result`);
throw new Error("Request did not return a result");
}

return this.deserialize(result);
Expand Down
40 changes: 40 additions & 0 deletions pkg/commands/json_arrappend.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { keygen, newHttpClient } from "../test-utils.ts";
import { afterAll } from "https://deno.land/[email protected]/testing/bdd.ts";

import { JsonSetCommand } from "./json_set.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { JsonArrAppendCommand } from "./json_arrappend.ts";
import { JsonGetCommand } from "./json_get.ts";

const client = newHttpClient();

const { newKey, cleanup } = keygen();
afterAll(cleanup);

Deno.test("Add a new color to a list of product colors", async () => {
const key = newKey();
const res1 = await new JsonSetCommand([key, "$", {
"name": "Noise-cancelling Bluetooth headphones",
"description":
"Wireless Bluetooth headphones with noise-cancelling technology",
"connection": { "wireless": true, "type": "Bluetooth" },
"price": 99.98,
"stock": 25,
"colors": ["black", "silver"],
}]).exec(client);
assertEquals(res1, "OK");
const res2 = await new JsonArrAppendCommand([key, "$.colors", '"blue"']).exec(
client,
);
assertEquals(res2, [3]);
const res3 = await new JsonGetCommand([key]).exec(client);
assertEquals(res3, {
"name": "Noise-cancelling Bluetooth headphones",
"description":
"Wireless Bluetooth headphones with noise-cancelling technology",
"connection": { "wireless": true, "type": "Bluetooth" },
"price": 99.98,
"stock": 25,
"colors": ["black", "silver", "blue"],
});
});
Loading