@crimson-education/replit-sdk 1.0.2-beta-15 → 1.0.2-beta-16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +85 -53
- package/lib/components/external-link.js +1 -1
- package/package.json +1 -1
package/Readme.md
CHANGED
|
@@ -1,75 +1,107 @@
|
|
|
1
|
-
|
|
1
|
+
# Replit SDK for Crimson App
|
|
2
2
|
|
|
3
3
|
This SDK facilitates the rapid integration of Replit-hosted applications into the Crimson ecosystem.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Core Principle
|
|
6
6
|
|
|
7
|
-
The integration
|
|
7
|
+
The integration works by passing environment variables and user parameters via URL parameters (query string or hash) to the Replit frontend. This data is stored in-memory and persists until the current session ends, providing a seamless iframe integration experience.
|
|
8
8
|
|
|
9
|
-
Project Structure
|
|
9
|
+
## Project Structure
|
|
10
10
|
|
|
11
|
-
- api
|
|
11
|
+
- **`api`**:
|
|
12
|
+
- `bindApi(app)`: Binds `/api/function` routes to an Express app for forwarding GraphQL requests.
|
|
13
|
+
- `graphqlProxySchema`: Zod schema for the proxy request body.
|
|
14
|
+
- **`components`**:
|
|
15
|
+
- `ExternalLink`: A specialized component designed to handle navigation to external URLs (out of iframe).
|
|
16
|
+
- **`hooks`**:
|
|
17
|
+
- `useUrlParams()`: Extracts and stores essential data (Auth Tokens, API endpoints) from the URL.
|
|
18
|
+
- `getStoredParam(key)` / `setStoredParams(values)`: Utilities for managing persisted parameters.
|
|
19
|
+
- **`functions`**:
|
|
20
|
+
- Implementations for fundamental Crimson API calls like `fetchLoginUser` and `fetchMyStudents`.
|
|
21
|
+
- **`utils`**:
|
|
22
|
+
- `initDatadog(config)`: Manages Datadog initialization.
|
|
23
|
+
- `trackEvent(name)`: Analytics utility.
|
|
24
|
+
- `apiRequest(query, variables)`: A pre-configured GraphQL request client wrapper.
|
|
12
25
|
|
|
13
|
-
|
|
26
|
+
## Quick Integration Guide
|
|
14
27
|
|
|
15
|
-
|
|
28
|
+
To integrate your Replit app into the Crimson ecosystem, follow these steps:
|
|
16
29
|
|
|
17
|
-
|
|
30
|
+
### 1. Install the SDK
|
|
18
31
|
|
|
19
|
-
|
|
32
|
+
```bash
|
|
33
|
+
npm install @crimson-education/replit-sdk@1.0.2-beta-15
|
|
34
|
+
```
|
|
20
35
|
|
|
21
|
-
|
|
36
|
+
### 2. Wrap your App with DevProvider
|
|
22
37
|
|
|
23
|
-
|
|
38
|
+
Initialize the context at the top level of your application.
|
|
24
39
|
|
|
25
|
-
|
|
40
|
+
```tsx
|
|
41
|
+
import { IDevProvider } from '@crimson-education/replit-sdk/lib/context/dev-context';
|
|
26
42
|
|
|
27
|
-
|
|
43
|
+
function App() {
|
|
44
|
+
return (
|
|
45
|
+
<IDevProvider>
|
|
46
|
+
<YourAppRoutes />
|
|
47
|
+
</IDevProvider>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
28
51
|
|
|
29
|
-
|
|
52
|
+
### 3. Setup Express Proxy
|
|
30
53
|
|
|
31
|
-
|
|
54
|
+
In your server-side code (e.g., `server/routes.ts`), bind the API proxy:
|
|
32
55
|
|
|
33
|
-
|
|
56
|
+
```typescript
|
|
57
|
+
import { bindApi } from '@crimson-education/replit-sdk';
|
|
34
58
|
|
|
35
|
-
|
|
59
|
+
export function registerRoutes(app: Express) {
|
|
60
|
+
bindApi(app);
|
|
61
|
+
// ... other routes
|
|
62
|
+
}
|
|
63
|
+
```
|
|
36
64
|
|
|
37
|
-
###
|
|
65
|
+
### 4. Use URL Parameters Hook
|
|
38
66
|
|
|
39
|
-
|
|
67
|
+
In your main component, use the hook to sync URL parameters to memory:
|
|
40
68
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
1. install and save package @crimson-education/replit-sdk@1.0.2-beta-14
|
|
44
|
-
2. wrap the App with the `IDevProvider` context, like
|
|
45
|
-
```
|
|
46
|
-
// ...
|
|
47
|
-
import { IDevProvider } from "@crimson-education/replit-sdk/lib/context/dev-context";
|
|
48
|
-
// ...
|
|
49
|
-
function App() {
|
|
50
|
-
return (
|
|
51
|
-
<IDevProvider>
|
|
52
|
-
<QueryClientProvider client={queryClient}>
|
|
53
|
-
<TooltipProvider>
|
|
54
|
-
<Toaster />
|
|
55
|
-
<Router />
|
|
56
|
-
</TooltipProvider>
|
|
57
|
-
</QueryClientProvider>
|
|
58
|
-
</IDevProvider>
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
3. set Express proxy endpint, call `bindApi(app)` to bind `/api/function` route in `server/routes.ts`
|
|
63
|
-
4. use the ExternalLink component to replace all the <a> tag which has a external link.
|
|
64
|
-
5. set dev capp endpoint on top of the app before any api call like ```
|
|
65
|
-
import { setStoredParams } from "@crimson-education/replit-sdk/lib/hooks/use-url-params";
|
|
66
|
-
setStoredParams({'cappEndpoint': 'http://6d94f1eb.r8.cpolar.top/graphql'});```
|
|
67
|
-
6. remove fetch button and staff userId input in every student picker dialog, and set getStoredUserId() as default staffId to load students once the dialog opened.
|
|
68
|
-
7. replace the fetchLoginUser and fetchMyStudent function, use the exported functions from the sdk and clean up the redundant server-side endpoints.
|
|
69
|
-
8. the fetchStudents api could return duplicated students, remove them to keep unique
|
|
70
|
-
9. init datadog, like: ```import { init as datadogInit } from "@crimson-education/replit-sdk/lib/utils/datadog";
|
|
71
|
-
datadogInit({ app: '{{here is the replit app name}}' });```
|
|
72
|
-
10. use trackEvent function to track all events that you may intrest, ```import { ExternalLink, trackEvent } from "@crimson-education/replit-sdk";
|
|
73
|
-
trackEvent('OPEN_COPILOT'); ```
|
|
69
|
+
```tsx
|
|
70
|
+
import { useUrlParams } from '@crimson-education/replit-sdk/lib/hooks/use-url-params';
|
|
74
71
|
|
|
75
|
-
|
|
72
|
+
function MainComponent() {
|
|
73
|
+
useUrlParams();
|
|
74
|
+
return <div>My Content</div>;
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 5. Handle External Links
|
|
79
|
+
|
|
80
|
+
Use the `ExternalLink` component for any links that should open outside the iframe:
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
import { ExternalLink } from '@crimson-education/replit-sdk';
|
|
84
|
+
|
|
85
|
+
<ExternalLink href="https://app.crimsoneducation.org">Go to Crimson App</ExternalLink>;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 6. Initialize Analytics (Optional)
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { init as datadogInit, trackEvent } from '@crimson-education/replit-sdk';
|
|
92
|
+
|
|
93
|
+
datadogInit({ app: 'your-replit-app-name' });
|
|
94
|
+
trackEvent('APP_LAUNCHED');
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 7. Fetch Crimson Data
|
|
98
|
+
|
|
99
|
+
Use the built-in functions to fetch user or student data:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { fetchLoginUser, fetchMyStudents } from '@crimson-education/replit-sdk/lib/functions';
|
|
103
|
+
|
|
104
|
+
const user = await fetchLoginUser();
|
|
105
|
+
// you may get multiple roles, just keep one of 'STRATEGIST', 'CASE_MANAGER', 'TUTOR',
|
|
106
|
+
const students = await fetchMyStudents(user.userId, ['STRATEGIST']);
|
|
107
|
+
```
|
|
@@ -8,7 +8,7 @@ const react_1 = __importDefault(require("react"));
|
|
|
8
8
|
const use_url_params_1 = require("../hooks/use-url-params");
|
|
9
9
|
const datadog_1 = require("../utils/datadog");
|
|
10
10
|
function ExternalLink({ href, replit, children, eventName = 'external_link_click', onClick, ...props }) {
|
|
11
|
-
const isReplit = replit !== null && replit !== void 0 ? replit : (href.
|
|
11
|
+
const isReplit = replit !== null && replit !== void 0 ? replit : (href.includes('replit.app') || href.includes('replit.com/t'));
|
|
12
12
|
const parentDomain = (0, use_url_params_1.getStoredParam)('parentDomain') || '';
|
|
13
13
|
const finalHref = isReplit && parentDomain ? parentDomain + '/replit/t?redirect=' + encodeURIComponent(href) : href;
|
|
14
14
|
const handleClick = (event) => {
|