跳转至

Add an OSDK package to an existing application(将 OSDK 包添加到现有应用程序)

This page will walk you through the process of adding an OSDK package to an existing application. If you do not have an existing application, view the documentation on bootstrapping a new OSDK application.

1: Prerequisites

Create a Developer Console application

Follow the steps listed in the create a new Developer Console application page.

Set up your token

Export your token in your local environment. Below is an example using a sample personal access token, but you can generate a longer-lived one in the Developer Console. This token should not be checked into source control because it is your personal access token.

export FOUNDRY_TOKEN=<YOUR-TOKEN-FROM-GETTING-STARTED-PAGE>

Check Node version

The Typescript SDK requires Node 18 or higher to work. To check what version of Node you are on, use the command below.

node --version

2: Add the OSDK to an existing Project

Set up the NPM registry

Add the following code to your repository or user .npmrc file, replacing any < >with an application-specific value:

//<REGISTRY-URL-FROM-OVERVIEW-PAGE>:_authToken=${FOUNDRY_TOKEN}
<PACKAGE-NAME>:registry=https://<REGISTRY-URL-FROM-OVERVIEW-PAGE>

Optional: Set up certificate

If your organization requires certificates for network traffic, you may need to tell Node where that certificate lives.

export NODE_EXTRA_CA_CERTS="/path/to/my.crt"

Install the latest version of the SDK

Run the following command to install your SDK package:

npm install <PACKAGE-NAME>@latest
npm install @osdk/client@latest
npm install @osdk/oauth@latest

Initialize the Foundry client to start developing

Add the following code to your application, replacing any < > with the specifics of your package:

import { createPublicOauthClient } from "@osdk/oauth";
import { createClient } from "@osdk/client";
import { <ANY-OBJECT-NAME> } from "<PACKAGE-NAME>";
import React, { useEffect } from "react";

const auth = createPublicOauthClient("<YOUR-CLIENT-ID>", "<YOUR-FOUNDRY-URL>", "<YOUR-REDIRECT-URL>");
const client: Client = createClient(u"<YOUR-FOUNDRY-URL>", "<YOUR-ONTOLOGY-RID>", auth);

export default function SimpleReactComponent() {
    useEffect(() => {
        if (auth.getTokenOrUndefined()) {
            auth.refresh().catch(() =>
            /**
               If we cannot refresh the token (for example, if the user is not logged in) we initiate the login flow in Foundry
               Once login is complete, the user will be redirected back to http://localhost:8080/auth/callback
            **/
            auth.signIn())
        } else {
            client(<ANY-OBJECT-NAME>).fetchPage({ $pageSize: 10 }).then((object) => {
                console.log(object.data);
            });
        }
    }, []);
};

中文翻译


将 OSDK 包添加到现有应用程序

本页将引导您完成将 OSDK 包添加到现有应用程序的过程。如果您没有现有应用程序,请查看引导新的 OSDK 应用程序的文档。

1:前提条件

创建 Developer Console 应用程序

按照创建新的 Developer Console 应用程序页面中的步骤操作。

设置令牌

在本地环境中导出您的令牌。以下示例使用了一个示例个人访问令牌(personal access token),但您可以在 Developer Console 中生成一个有效期更长的令牌。由于这是您的个人访问令牌,因此不应将其检入源代码管理。

export FOUNDRY_TOKEN=<YOUR-TOKEN-FROM-GETTING-STARTED-PAGE>

检查 Node 版本

Typescript SDK 需要 Node 18 或更高版本才能运行。要检查您当前使用的 Node 版本,请使用以下命令。

node --version

2:将 OSDK 添加到现有项目

设置 NPM 注册表

将以下代码添加到您的仓库或用户的 .npmrc 文件中,并将所有 < > 替换为特定于应用程序的值:

//<REGISTRY-URL-FROM-OVERVIEW-PAGE>:_authToken=${FOUNDRY_TOKEN}
<PACKAGE-NAME>:registry=https://<REGISTRY-URL-FROM-OVERVIEW-PAGE>

可选:设置证书

如果您的组织要求对网络流量使用证书,您可能需要告知 Node 证书的位置。

export NODE_EXTRA_CA_CERTS="/path/to/my.crt"

安装最新版本的 SDK

运行以下命令来安装您的 SDK 包:

npm install <PACKAGE-NAME>@latest
npm install @osdk/client@latest
npm install @osdk/oauth@latest

初始化 Foundry 客户端以开始开发

将以下代码添加到您的应用程序中,并将所有 < > 替换为您包的具体信息:

import { createPublicOauthClient } from "@osdk/oauth";
import { createClient } from "@osdk/client";
import { <ANY-OBJECT-NAME> } from "<PACKAGE-NAME>";
import React, { useEffect } from "react";

const auth = createPublicOauthClient("<YOUR-CLIENT-ID>", "<YOUR-FOUNDRY-URL>", "<YOUR-REDIRECT-URL>");
const client: Client = createClient(u"<YOUR-FOUNDRY-URL>", "<YOUR-ONTOLOGY-RID>", auth);

export default function SimpleReactComponent() {
    useEffect(() => {
        if (auth.getTokenOrUndefined()) {
            auth.refresh().catch(() =>
            /**
               如果无法刷新令牌(例如,用户未登录),我们将在 Foundry 中启动登录流程
               登录完成后,用户将被重定向回 http://localhost:8080/auth/callback
            **/
            auth.signIn())
        } else {
            client(<ANY-OBJECT-NAME>).fetchPage({ $pageSize: 10 }).then((object) => {
                console.log(object.data);
            });
        }
    }, []);
};