@sparkrewards/sra-client 0.0.3 → 0.0.4
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 +87 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,31 +1,106 @@
|
|
|
1
|
-
## @sparkrewards/
|
|
1
|
+
## @sparkrewards/sra-client
|
|
2
2
|
|
|
3
3
|
TypeScript / JavaScript client SDK for the Spark Rewards App API, generated from the Smithy service model.
|
|
4
4
|
|
|
5
5
|
### Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @sparkrewards/
|
|
8
|
+
npm install @sparkrewards/sra-client
|
|
9
9
|
# or
|
|
10
|
-
yarn add @sparkrewards/
|
|
10
|
+
yarn add @sparkrewards/sra-client
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
###
|
|
13
|
+
### Authentication
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
The App API uses **Cognito User Pools** authentication. You must configure the client with the `aws.auth#cognitoUserPools` auth scheme.
|
|
16
|
+
|
|
17
|
+
> **Important:** The generated client does not automatically provide auth scheme implementations. You must configure `httpAuthSchemes` when creating the client.
|
|
18
|
+
|
|
19
|
+
#### Basic Usage with AWS Amplify
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { AppAPIClient } from '@sparkrewards/sra-client';
|
|
23
|
+
import { fetchAuthSession } from 'aws-amplify/auth';
|
|
24
|
+
import {
|
|
25
|
+
HttpAuthScheme,
|
|
26
|
+
Identity,
|
|
27
|
+
IdentityProvider,
|
|
28
|
+
IdentityProviderConfig
|
|
29
|
+
} from "@smithy/types";
|
|
30
|
+
|
|
31
|
+
interface CognitoIdentity extends Identity {
|
|
32
|
+
token: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function createCognitoAuthScheme(token?: string): HttpAuthScheme {
|
|
36
|
+
return {
|
|
37
|
+
schemeId: "aws.auth#cognitoUserPools",
|
|
38
|
+
identityProvider: (config: IdentityProviderConfig): IdentityProvider<CognitoIdentity> => {
|
|
39
|
+
return async (): Promise<CognitoIdentity> => {
|
|
40
|
+
return { token: token || '' } as CognitoIdentity;
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
signer: {
|
|
44
|
+
sign: async (httpRequest: any, identity: any) => {
|
|
45
|
+
const cognitoIdentity = identity as CognitoIdentity;
|
|
46
|
+
httpRequest.headers["Authorization"] = `Bearer ${cognitoIdentity.token}`;
|
|
47
|
+
return httpRequest;
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function getClient(): Promise<AppAPIClient> {
|
|
54
|
+
const session = await fetchAuthSession();
|
|
55
|
+
const idToken = session.tokens?.idToken?.toString();
|
|
56
|
+
|
|
57
|
+
const client = new AppAPIClient({
|
|
58
|
+
endpoint: 'https://beta-api.sparkrewards.co/sra',
|
|
59
|
+
httpAuthSchemes: [createCognitoAuthScheme(idToken)],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return client;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Usage
|
|
66
|
+
const client = await getClient();
|
|
67
|
+
const result = await client.send(new GetUserCommand({}));
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### Using with Public Endpoints
|
|
71
|
+
|
|
72
|
+
For public endpoints (those with `@auth([])` in the model), you don't need authentication:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { AppAPIClient, NearbyShopsPublicCommand } from '@sparkrewards/sra-client';
|
|
17
76
|
|
|
18
77
|
const client = new AppAPIClient({
|
|
19
|
-
|
|
20
|
-
|
|
78
|
+
endpoint: 'https://beta-api.sparkrewards.co/sra',
|
|
79
|
+
httpAuthSchemes: [], // Empty array for public-only usage
|
|
21
80
|
});
|
|
22
81
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
82
|
+
const shops = await client.send(new NearbyShopsPublicCommand({
|
|
83
|
+
lat: 40.7128,
|
|
84
|
+
lon: -74.0060,
|
|
85
|
+
}));
|
|
27
86
|
```
|
|
28
87
|
|
|
88
|
+
### API Operations
|
|
89
|
+
|
|
90
|
+
The client supports both authenticated and public operations:
|
|
91
|
+
|
|
92
|
+
**Authenticated operations** (require Cognito token):
|
|
93
|
+
- `GetUser`, `UpdateUser`, `DeleteUser`, `OnboardUser`
|
|
94
|
+
- `NearbyShops`, `FavoriteShops`, `GetShop`, `PinnedShop`, `PopularShops`
|
|
95
|
+
- `Plan`, `Plans`, `FavoritePlans`
|
|
96
|
+
- `RedeemFirstTimeReward`, `RedeemVisitReward`, `RedeemPointReward`
|
|
97
|
+
- `RecordVisit`, `LikeOrg`
|
|
98
|
+
|
|
99
|
+
**Public operations** (no authentication required):
|
|
100
|
+
- `NearbyShopsPublic`, `PopularShopsPublic`, `GetShopPublic`
|
|
101
|
+
- `PlanPublic`, `PinnedShopPublic`, `SearchShopsPublic`
|
|
102
|
+
- `RadiusShops`, `SearchSuggestion`
|
|
103
|
+
|
|
29
104
|
### Regenerating from the Smithy model
|
|
30
105
|
|
|
31
106
|
This package is generated by the `typescript-client-codegen` Smithy plugin. To regenerate it:
|
|
@@ -38,4 +113,3 @@ The generated package lives under:
|
|
|
38
113
|
|
|
39
114
|
- `smithy/build/smithyprojections/smithy/source/typescript-client-codegen`
|
|
40
115
|
|
|
41
|
-
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sparkrewards/sra-client",
|
|
3
3
|
"description": "@sparkrewards/sra-client client",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
|
|
7
7
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|