> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/migration/migration.getoriginstorage.md).

# Migration.getOriginStorage

### 功能说明

上一个 `web.tossmini.com` Origin 和当前 `apps.tossmini.com` 会一并获取 Origin 的 localStorage、IndexedDB、OPFS 数据。获取到的数据请按应用规则自行迁移或合并。

### 类型

没有参数。

```ts
Migration.getOriginStorage(): Promise<{
  previous: OriginStorageDump;
  current: OriginStorageDump;
}>;
```

`上一个`和 `当前`的结构相同。 `上一个`其中放的是之前的 `web.tossmini.com` Origin 的值， `当前`其中放的是当前的 `apps.tossmini.com` Origin 的值。

```ts
interface OriginStorageDump {
  /** 读取此快照的 Origin。 */
  origin: string;

  /** localStorage 的所有键和原始字符串值。 */
  localStorage: Record<string, string | null>;

  /** Origin 中的 IndexedDB 数据库列表。 */
  indexedDB: Array<{
    /** 数据库名称。 */
    name: string;
    /** 数据库版本。 */
    version: number;
    /** 数据库中的对象存储列表。 */
    objectStores: Array<{
      /** 对象存储名称。 */
      name: string;
      /** 对象存储的 keyPath。若是 out-of-line 键，则为 null。 */
      keyPath: string | string[] | null;
      /** 表示是否为自动生成键的对象存储。 */
      autoIncrement: boolean;
      /** 对象存储中定义的索引列表。 */
      indexes: Array<{
        name: string;
        keyPath: string | string[];
        multiEntry: boolean;
        unique: boolean;
      }>;
      /** 存储在对象存储中的所有记录。 */
      records: Array<{
        /** 从游标中读取的记录键。 */
        key: IDBValidKey;
        /** 记录所指向的对象存储的主键。 */
        primaryKey: IDBValidKey;
        /** 已保存的值。使用前需要按应用的类型进行校验。 */
        value: unknown;
      }>;
    }>;
  }>;

  /** 以 OPFS 根目录为基准的目录和文件列表。 */
  opfs: {
    /** 相对于根目录的路径。路径前不会带斜杠。 */
    directories: string[];
    files: Array<{
      /** 相对于根目录的文件路径。 */
      path: string;
      /** 文件的 MIME 类型。未知时可能为空字符串。 */
      type: string;
      /** 最后修改时间。以 Unix epoch 为基准的毫秒值。 */
      lastModified: number;
      /** 文件的原始字节。字符串文件可通过 TextDecoder 读取。 */
      data: ArrayBuffer;
    }>;
  };

  /** 各存储的读取失败信息。若为空数组，表示三个存储都已读取。 */
  errors: Array<{
    storage: "localStorage" | "indexedDB" | "opfs";
    message: string;
  }>;
}
```

`OriginStorageDump`可直接从包中导入。

```ts
import type { OriginStorageDump } from "@apps-in-toss/web-framework";
```

如果调用本身失败，Promise 会被 reject。若只有部分存储读取失败，Promise 会 resolve，原因会记录在 `previous.errors` 或 `current.errors`中。

### 示例代码

下面的示例仅在没有读取错误且当前 Origin 中没有值时，才会迁移之前的 localStorage 值。如果两边都有值，则保留当前值。

```ts
import { Migration } from "@apps-in-toss/web-framework";

const SETTINGS_KEY = "my-app:settings";

async function migrateSettings() {
  const { previous, current } = await Migration.getOriginStorage();

  if (previous.errors.length > 0 || current.errors.length > 0) {
    console.error("未能读取所有存储。", {
      previous: previous.errors,
      current: current.errors,
    });
    return;
  }

  const previousValue = previous.localStorage[SETTINGS_KEY];
  const currentValue = current.localStorage[SETTINGS_KEY];

  if (currentValue == null && previousValue != null) {
    localStorage.setItem(SETTINGS_KEY, previousValue);
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/migration/migration.getoriginstorage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
