Skip to content

Commit

Permalink
fix: browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
piloking committed Dec 30, 2024
1 parent 30c36e7 commit e127a4c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BaseStorage, type Storage } from "./base.ts";
import { type Kv, openKv } from "@deno/kv";
import {
BaseStorage,
type Storage,
} from "../../packages/linejs/src/storage/base.ts";
import { type Kv, openKv } from "npm:@deno/kv";

/**
* @lassdesc Deno.Kv Storage for LINE Client
Expand Down
46 changes: 46 additions & 0 deletions example/storage/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// <reference lib="dom"/>

import type {
BaseStorage,
Storage,
} from "../../packages/linejs/src/storage/base.ts";
export class LocalStorage implements BaseStorage {
prefix = "linejs:";
constructor() {
}
public async set(
key: Storage["Key"],
value: Storage["Value"],
): Promise<void> {
localStorage.setItem(this.prefix + key, JSON.stringify(value));
}
public async get(
key: Storage["Key"],
): Promise<Storage["Value"] | undefined> {
try {
return JSON.parse(
localStorage.getItem(this.prefix + key) || "null",
);
} catch (_) {
}
}
public async delete(key: Storage["Key"]): Promise<void> {
localStorage.removeItem(this.prefix + key);
}
public async clear(): Promise<void> {
localStorage.clear();
}
public async migrate(storage: BaseStorage): Promise<void> {
for (let index = 0; index < localStorage.length; index++) {
const k = localStorage.key(index);
if (k) {
storage.set(
k.replace(this.prefix, ""),
localStorage.getItem(k),
);
} else {
continue;
}
}
}
}
2 changes: 1 addition & 1 deletion migrate_to_v2.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## What changed
## What's changed

### Client

Expand Down
1 change: 0 additions & 1 deletion packages/linejs/src/storage/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export { BaseStorage } from "./base.ts";
export { DirStorage } from "./dir.ts";
export { FileStorage } from "./file.ts";
export { MemoryStorage } from "./memory.ts";
export { DenoKvStorage } from "./denokv.ts";

0 comments on commit e127a4c

Please sign in to comment.