npm package: @sparkrewards/srw-client
Description: @sparkrewards/srw-client client
Latest version: 0.1.3

## @sparkrewards/srw-client

TypeScript / JavaScript client SDK for the Spark Rewards Website (SRW) API, generated from the Smithy service model.

### Installation

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

### Authentication

The Spark Rewards Website (SRW) 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 { BusinessAPIClient } from '@sparkrewards/srw-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<BusinessAPIClient> {
  const session = await fetchAuthSession();
  const idToken = session.tokens?.idToken?.toString();

  const client = new BusinessAPIClient({
    endpoint: 'https://beta-api.sparkrewards.co/srw',
    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 { BusinessAPIClient, NearbyShopsPublicCommand } from '@sparkrewards/srw-client';

const client = new BusinessAPIClient({
  endpoint: 'https://beta-api.sparkrewards.co/srw',
  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`