npm package: @sparkrewards/sra-client
Description: @sparkrewards/sra-client client
Latest version: 0.0.10

## @sparkrewards/sra-client

TypeScript / JavaScript client SDK for the Spark Rewards App API, generated from the Smithy service model.

### Installation

```bash
npm install @sparkrewards/sra-client
# or
yarn add @sparkrewards/sra-client
```

### Authentication

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 `httpAuthSchemes` when creating the client.

#### Basic Usage with AWS Amplify

```typescript
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:

```typescript
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`, `OnboardUser`
- `NearbyShops`, `FavoriteShops`, `GetShop`, `PinnedShop`, `PopularShops`
- `Plan`, `Plans`, `FavoritePlans`
- `RedeemFirstTimeReward`, `RedeemVisitReward`, `RedeemPointReward`
- `RecordVisit`, `LikeOrg`

**Public operations** (no authentication required):
- `NearbyShopsPublic`, `PopularShopsPublic`, `GetShopPublic`
- `PlanPublic`, `PinnedShopPublic`, `SearchShopsPublic`
- `RadiusShops`, `SearchSuggestion`

### Regenerating from the Smithy model

This package is generated by the `typescript-client-codegen` Smithy plugin. To regenerate it:

```bash
./gradlew :smithy:smithyBuild
```

The generated package lives under:

- `smithy/build/smithyprojections/smithy/source/typescript-client-codegen`