@sparkrewards/sra-client
v0.0.4
Published
@sparkrewards/sra-client client
Readme
@sparkrewards/sra-client
TypeScript / JavaScript client SDK for the Spark Rewards App API, generated from the Smithy service model.
Installation
npm install @sparkrewards/sra-client
# or
yarn add @sparkrewards/sra-clientAuthentication
The App API uses Cognito User Pools authentication. You must configure the client with the aws.auth#cognitoUserPools auth scheme.
Important: The generated client does not automatically provide auth scheme implementations. You must configure
httpAuthSchemeswhen creating the client.
Basic Usage with AWS Amplify
import { AppAPIClient } from '@sparkrewards/sra-client';
import { fetchAuthSession } from 'aws-amplify/auth';
import {
HttpAuthScheme,
Identity,
IdentityProvider,
IdentityProviderConfig
} from "@smithy/types";
interface CognitoIdentity extends Identity {
token: string;
}
function createCognitoAuthScheme(token?: string): HttpAuthScheme {
return {
schemeId: "aws.auth#cognitoUserPools",
identityProvider: (config: IdentityProviderConfig): IdentityProvider<CognitoIdentity> => {
return async (): Promise<CognitoIdentity> => {
return { token: token || '' } as CognitoIdentity;
};
},
signer: {
sign: async (httpRequest: any, identity: any) => {
const cognitoIdentity = identity as CognitoIdentity;
httpRequest.headers["Authorization"] = `Bearer ${cognitoIdentity.token}`;
return httpRequest;
},
},
};
}
async function getClient(): Promise<AppAPIClient> {
const session = await fetchAuthSession();
const idToken = session.tokens?.idToken?.toString();
const client = new AppAPIClient({
endpoint: 'https://beta-api.sparkrewards.co/sra',
httpAuthSchemes: [createCognitoAuthScheme(idToken)],
});
return client;
}
// Usage
const client = await getClient();
const result = await client.send(new GetUserCommand({}));Using with Public Endpoints
For public endpoints (those with @auth([]) in the model), you don't need authentication:
import { AppAPIClient, NearbyShopsPublicCommand } from '@sparkrewards/sra-client';
const client = new AppAPIClient({
endpoint: 'https://beta-api.sparkrewards.co/sra',
httpAuthSchemes: [], // Empty array for public-only usage
});
const shops = await client.send(new NearbyShopsPublicCommand({
lat: 40.7128,
lon: -74.0060,
}));API Operations
The client supports both authenticated and public operations:
Authenticated operations (require Cognito token):
GetUser,UpdateUser,DeleteUser,OnboardUserNearbyShops,FavoriteShops,GetShop,PinnedShop,PopularShopsPlan,Plans,FavoritePlansRedeemFirstTimeReward,RedeemVisitReward,RedeemPointRewardRecordVisit,LikeOrg
Public operations (no authentication required):
NearbyShopsPublic,PopularShopsPublic,GetShopPublicPlanPublic,PinnedShopPublic,SearchShopsPublicRadiusShops,SearchSuggestion
Regenerating from the Smithy model
This package is generated by the typescript-client-codegen Smithy plugin. To regenerate it:
./gradlew :smithy:smithyBuildThe generated package lives under:
smithy/build/smithyprojections/smithy/source/typescript-client-codegen
