Skip to content

Commit 83819e3

Browse files
authored
feat: Profile api integration (#496)
1 parent 2fd1de3 commit 83819e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+5294
-4
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,9 @@ GASLESS_SERVICE_URL=
117117

118118
# Required for push notifications from the Notification Sender Service
119119
NOTIFICATION_SENDER_SERVICE_URL=
120+
121+
# Required for getting the balances from the Balance service
122+
BALANCE_SERVICE_URL=https://core-balance-api.avax-test.network
123+
124+
# Required for interacting with the profile service
125+
PROFILE_SERVICE_URL=https://core-profile-api.avax-test.network

.github/workflows/e2e_testing.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ jobs:
5050
echo 'EXTENSION_PUBLIC_KEY="${{ secrets.EXTENSION_PUBLIC_KEY_E2E }}"' >> .env.production
5151
echo 'ID_SERVICE_API_KEY="${{ secrets.ID_SERVICE_API_KEY }}"' >> .env.production
5252
echo 'NOTIFICATION_SENDER_SERVICE_URL="${{ secrets.NOTIFICATION_SENDER_SERVICE_URL }}"' >> .env.production
53+
echo 'BALANCE_SERVICE_URL="${{ secrets.BALANCE_SERVICE_URL }}"' >> .env.production
54+
echo 'PROFILE_SERVICE_URL="${{ secrets.PROFILE_SERVICE_URL }}"' >> .env.production
5355
- name: Install dependencies
5456
run: |
5557
yarn install

.github/workflows/main_branch.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ jobs:
5151
echo 'NOTIFICATION_SENDER_SERVICE_URL="${{ secrets.NOTIFICATION_SENDER_SERVICE_URL }}"' >> .env.production
5252
echo 'EXTENSION_PUBLIC_KEY="${{ secrets.EXTENSION_PUBLIC_KEY }}"' >> .env.production
5353
echo 'NEXT_GEN_EXTENSION_PUBLIC_KEY="${{ secrets.NEXT_GEN_EXTENSION_PUBLIC_KEY }}"' >> .env.production
54+
echo 'BALANCE_SERVICE_URL="${{ secrets.BALANCE_SERVICE_URL }}"' >> .env.production
55+
echo 'PROFILE_SERVICE_URL="${{ secrets.PROFILE_SERVICE_URL }}"' >> .env.production
5456
- name: Install dependencies
5557
run: |
5658
yarn install

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ yarn dev
6868
4. Click `Load unpacked` and go to the extension folder.
6969
5. Select the `dist` folder and press `Select`.
7070

71+
### Update api clients
72+
73+
Please refer to the **[DOC](/packages/service-worker/src/api-clients/README.md)**
74+
7175
## Release
7276

7377
### Versioning

eslint.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ export default [
7979
],
8080
},
8181
},
82+
{
83+
// ignoring files generated by @hey-api
84+
ignores: ['**/*.gen.ts'],
85+
},
8286
];

openapi-ts.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from '@hey-api/openapi-ts';
2+
3+
export default defineConfig([
4+
// multiple config can go here
5+
{
6+
input: 'https://core-balance-api.avax.network/schema.json',
7+
output: 'packages/service-worker/src/api-clients/balance-api',
8+
},
9+
{
10+
input: 'https://core-profile-api.avax.network/schema.json',
11+
output: 'packages/service-worker/src/api-clients/profile-api',
12+
},
13+
]);

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"build:alpha:no-source-maps": "NO_SOURCE_MAPS=true yarn build:alpha",
2020
"lint": "yarn workspaces foreach -Ap --exclude @core-ext/next run lint",
2121
"lint:next": "yarn workspaces foreach -Ap --exclude @core-ext/legacy run lint",
22+
"generate-clients": "openapi-ts && yarn workspace @core/service-worker api-client:prepend && yarn workspace @core/service-worker prettify",
2223
"postinstall": "husky install",
2324
"prettify": "yarn workspaces foreach -Ap run prettify",
2425
"typecheck": "yarn workspaces foreach -Ap --exclude @core-ext/next run typecheck",
@@ -47,6 +48,7 @@
4748
"@eslint/compat": "1.2.4",
4849
"@eslint/eslintrc": "3.2.0",
4950
"@eslint/js": "9.16.0",
51+
"@hey-api/openapi-ts": "0.87.5",
5052
"@lavamoat/allow-scripts": "2.0.0",
5153
"@lavamoat/preinstall-always-fail": "1.0.0",
5254
"@rsbuild/core": "1.3.3",
@@ -78,7 +80,7 @@
7880
},
7981
"lint-staged": {
8082
"**/*": "prettier --write --ignore-unknown",
81-
"*.{ts,tsx}": "eslint --fix --max-warnings 0"
83+
"*.{ts,tsx}": "eslint --fix --max-warnings 0 --ignore-pattern '**/*.gen.ts' --no-warn-ignored"
8284
},
8385
"volta": {
8486
"node": "20.18.0",

packages/service-worker/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
preset: 'ts-jest',
66
resolver: '<rootDir>/../../src/tests/resolver.js',
77
testEnvironment: 'jest-environment-jsdom',
8+
setupFiles: ['<rootDir>/../../src/tests/mockClientApis.ts'],
89
setupFilesAfterEnv: ['<rootDir>/../../src/tests/setupTests.ts'],
910
moduleNameMapper: {
1011
'^~/(.*)': '<rootDir>/src/$1',

packages/service-worker/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
"dev:next": "rsbuild build -w --config rsbuild.worker.dev.ts -- --core-gen=next",
99
"build": "rsbuild build --config rsbuild.worker.prod.ts",
1010
"build:next": "rsbuild build --config rsbuild.worker.prod.ts -- --core-gen=next",
11-
"lint": "eslint --fix \"src/**/*.ts*\"",
11+
"lint": "eslint --fix \"src/**/*.ts*\" --no-warn-ignored",
1212
"prettify": "prettier --write \"src/**/*.ts*\"",
1313
"test": "yarn jest",
1414
"test:watch": "yarn jest --watch",
1515
"test:path": "yarn jest --watch --testPathPattern",
16+
"api-client:prepend": "node src/api-clients/prepend-ts-nocheck.mjs",
1617
"typecheck": "yarn tsc --skipLibCheck --noEmit"
1718
},
1819
"dependencies": {
@@ -145,6 +146,7 @@
145146
"ethers": "6.13.5",
146147
"file-loader": "6.2.0",
147148
"fs": "0.0.1-security",
149+
"glob": "13.0.0",
148150
"globals": "15.13.0",
149151
"husky": "7.0.4",
150152
"i18next-scanner": "4.1.1",
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# How to generate the api clients
2+
3+
First before diving in on the flow of "version control" in terms of the api client a quick start on how to generate them
4+
5+
Essentially you just need to run the `generate-clients` script from the root `package.json`
6+
7+
This will use the configuration from `openapi-ts.config.ts`. This tells **[@hey-api](https://heyapi.dev/openapi-ts/clients)** from where it should get the `schema.json` and where to put the generated clients
8+
9+
If you need a client to a new service, just add a new config object with the `input-output` pair to the array
10+
11+
# Version control with feature flags
12+
13+
If there is a new feature, or you are updating an existing API you might need to update the current client
14+
15+
If the given service's updated API response is backwards compatible you can just run the aforementioned `generate-clients` and you are good to go
16+
17+
## In case of breaking change
18+
19+
In order to be able to release the extension with the changes without the need to wait for the API deployed first we need to have the old and the new version of the client live next to eachother.
20+
21+
In such cases you should modify the already existing `openapi-ts.config.ts` config of the client to generate the old client to a `deprecated` folder:
22+
e.g.:
23+
24+
```javascript
25+
import { defineConfig } from '@hey-api/openapi-ts';
26+
27+
export default defineConfig([
28+
// multiple config can go here
29+
{
30+
input: 'https://core-balance-api.avax.network/schema.json',
31+
output: 'packages/service-worker/src/api-clients/deprecated/balance-api', // <-- additional deprecated slug
32+
},
33+
{
34+
input: 'https://core-profile-api.avax.network/schema.json',
35+
output: 'packages/service-worker/src/api-clients/profile-api',
36+
},
37+
]);
38+
```
39+
40+
After running `generate-clients` you should then update the reference in `packages/service-worker/src/api-clients/clients.ts` to point to the correct folder for the old version
41+
42+
```javascript
43+
import { createClient as createV1BalanceApiClient } from '~/api-clients/deprecated/balance-api/client';
44+
```
45+
46+
After this you should create the new version of the client by adding the config to the `openapi-ts.config.ts` and running `generate-clients`
47+
48+
```javascript
49+
import { defineConfig } from '@hey-api/openapi-ts';
50+
51+
export default defineConfig([
52+
// multiple config can go here
53+
{
54+
input: 'https://core-balance-api.avax-test.network/schema.json', // <-- pointing to staging
55+
output: 'packages/service-worker/src/api-clients/balance-api',
56+
},
57+
{
58+
input: 'https://core-balance-api.avax.network/schema.json',
59+
output: 'packages/service-worker/src/api-clients/deprecated/balance-api',
60+
},
61+
{
62+
input: 'https://core-profile-api.avax.network/schema.json',
63+
output: 'packages/service-worker/src/api-clients/profile-api',
64+
},
65+
]);
66+
```
67+
68+
The last step is to add this client to the `packages/service-worker/src/api-clients/clients.ts` file
69+
70+
```javascript
71+
import { container } from 'tsyringe';
72+
import { AppCheckService } from '~/services/appcheck/AppCheckService';
73+
import { createClient as createV1ProfileApiClient } from '~/api-clients/profile-api/client';
74+
import { createClient as createV1BalanceApiClient } from '~/api-clients/deprecated/balance-api/client';
75+
import { createClient as createV2BalanceApiClient } from '~/api-clients/balance-api/client';
76+
77+
// The rest
78+
79+
const balanceApiClientV2 = createV2BalanceApiClient({
80+
baseUrl: process.env.BALANCE_SERVICE_URL,
81+
});
82+
balanceApiClientV2.interceptors.request.use(authInterceptor);
83+
84+
export {
85+
profileApiClientV1 as profileApiClient,
86+
balanceApiClientV1 as balanceApiClient,
87+
balanceApiClientV2,
88+
};
89+
```
90+
91+
## Usage
92+
93+
In the code than based on feature flags you can either use the old client or the new version of it.
94+
95+
## Cleanup
96+
97+
Once the integration/migration is done you can just update the exports in `packages/service-worker/src/api-clients/clients.ts` and cleanup the logic around feature flags in the code base
98+
99+
```javascript
100+
import { container } from 'tsyringe';
101+
import { AppCheckService } from '~/services/appcheck/AppCheckService';
102+
import { createClient as createV1ProfileApiClient } from '~/api-clients/profile-api/client';
103+
import { createClient as createV2BalanceApiClient } from '~/api-clients/balance-api/client';
104+
105+
// The rest
106+
107+
const balanceApiClientV2 = createV2BalanceApiClient({
108+
baseUrl: process.env.BALANCE_SERVICE_URL,
109+
});
110+
balanceApiClientV2.interceptors.request.use(authInterceptor);
111+
112+
export {
113+
profileApiClientV1 as profileApiClient,
114+
balanceApiClientV2 as balanceApiClient,
115+
};
116+
```

0 commit comments

Comments
 (0)