revenuecat-api 1.0.2
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/.github/workflows/ci.yml +37 -0
- package/.github/workflows/publish.yml +34 -0
- package/LICENSE +21 -0
- package/README.md +191 -0
- package/dist/__generated/revenuecat-api-v2.d.ts +5110 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +68 -0
- package/dist/rateLimitMiddleware.d.ts +3 -0
- package/dist/rateLimitMiddleware.d.ts.map +1 -0
- package/dist/rateLimitMiddleware.js +160 -0
- package/eslint.config.mjs +11 -0
- package/package.json +43 -0
- package/pnpm-workspace.yaml +2 -0
- package/scripts/generate_openapi_client.sh +9 -0
- package/test/rateLimitMiddleware.test.ts +609 -0
- package/tsconfig.json +114 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
|
|
2
|
+
|
|
3
|
+
name: CI
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: ["*"]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
node-version: [20]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- name: Install pnpm
|
|
19
|
+
uses: pnpm/action-setup@v4
|
|
20
|
+
|
|
21
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
22
|
+
uses: actions/setup-node@v4
|
|
23
|
+
with:
|
|
24
|
+
node-version: ${{ matrix.node-version }}
|
|
25
|
+
cache: "pnpm"
|
|
26
|
+
|
|
27
|
+
- name: Install dependencies
|
|
28
|
+
run: pnpm install
|
|
29
|
+
|
|
30
|
+
- name: Run lint
|
|
31
|
+
run: pnpm lint
|
|
32
|
+
|
|
33
|
+
- name: Run prepack
|
|
34
|
+
run: pnpm prepack
|
|
35
|
+
|
|
36
|
+
- name: Run tests
|
|
37
|
+
run: pnpm test
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
|
|
2
|
+
|
|
3
|
+
name: Publish to npm
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
release:
|
|
7
|
+
types: [published]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
publish:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
|
|
15
|
+
- name: Install pnpm
|
|
16
|
+
uses: pnpm/action-setup@v4
|
|
17
|
+
|
|
18
|
+
- name: Setup Node.js
|
|
19
|
+
uses: actions/setup-node@v4
|
|
20
|
+
with:
|
|
21
|
+
node-version: "20.x"
|
|
22
|
+
registry-url: "https://registry.npmjs.org"
|
|
23
|
+
cache: "pnpm"
|
|
24
|
+
|
|
25
|
+
- name: Install dependencies
|
|
26
|
+
run: pnpm install
|
|
27
|
+
|
|
28
|
+
- name: Set publishing config
|
|
29
|
+
run: pnpm config set '//registry.npmjs.org/:_authToken' "${NODE_AUTH_TOKEN}"
|
|
30
|
+
env:
|
|
31
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
|
32
|
+
|
|
33
|
+
- name: Publish to npm
|
|
34
|
+
run: pnpm publish --access public --no-git-checks
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 David Idol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# RevenueCat API Client
|
|
2
|
+
|
|
3
|
+
A type-safe, isomorphic API client for the RevenueCat v2 REST API.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides a lightweight, fully-typed client for interacting with the RevenueCat API. It's generated from the [official RevenueCat OpenAPI specification](https://www.revenuecat.com/docs/api-v2) and built on top of [`openapi-typescript`](https://openapi-ts.dev/) and [`openapi-fetch`](https://openapi-ts.dev/openapi-fetch/), offering significantly smaller bundle sizes compared to traditional code generators like `openapi-generator`.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Type Safety**: Full TypeScript support with generated types from the official OpenAPI spec
|
|
12
|
+
- **Automatic Rate Limiting**: Built-in rate limiting that respects RevenueCat's `Retry-After` headers
|
|
13
|
+
- **Isomorphic**: Works in both server-side (Node.js, Deno) and browser environments
|
|
14
|
+
- **Lightweight**: Minimal bundle size using modern fetch-based architecture
|
|
15
|
+
- **Queue Management**: Intelligent request queuing to handle rate limits gracefully
|
|
16
|
+
- **Configurable**: Customizable options for different use cases
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add revenuecat-api
|
|
22
|
+
# or
|
|
23
|
+
yarn add revenuecat-api
|
|
24
|
+
# or
|
|
25
|
+
npm install revenuecat-api
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { createRevenueCatClient } from 'revenuecat-api';
|
|
32
|
+
|
|
33
|
+
// Create a client with your RevenueCat API key
|
|
34
|
+
const client = createRevenueCatClient('your-api-key-here');
|
|
35
|
+
|
|
36
|
+
// Example: Fetch subscription information
|
|
37
|
+
// (Note all API endpoint strings are type-safe and discoverable via IntelliSense!)
|
|
38
|
+
const { data, error } = await client[
|
|
39
|
+
"/projects/{project_id}/customers/{customer_id}/subscriptions"
|
|
40
|
+
].GET({
|
|
41
|
+
params: {
|
|
42
|
+
// Request path, query, and body are type checked
|
|
43
|
+
path: {
|
|
44
|
+
project_id: "proj1ab2c3d4",
|
|
45
|
+
customer_id: "19b8de26-77c1-49f1-aa18-019a391603e2",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Response data is typed as well
|
|
51
|
+
if (error) {
|
|
52
|
+
console.error(`Error of type ${error.type}:`, error.message);
|
|
53
|
+
} else {
|
|
54
|
+
console.log('Subscription data:', data);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Rate Limiting
|
|
59
|
+
|
|
60
|
+
The client can optionally handle RevenueCat's rate limiting by:
|
|
61
|
+
|
|
62
|
+
- Respecting `Retry-After` headers from 429 responses
|
|
63
|
+
- Queuing requests when rate limits are hit
|
|
64
|
+
- Automatically retrying failed requests (up to 3 times by default)
|
|
65
|
+
- Managing per-endpoint rate limit states
|
|
66
|
+
|
|
67
|
+
You can opt into automatic rate limiting if desired:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
const client = createRevenueCatClient('your-api-key', {
|
|
71
|
+
automaticRateLimit: true
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
For more details on RevenueCat's rate limiting, see the [official documentation](https://www.revenuecat.com/docs/api-v2#tag/Rate-Limit).
|
|
76
|
+
|
|
77
|
+
## API Reference
|
|
78
|
+
|
|
79
|
+
### `createRevenueCatClient(accessToken, options?)`
|
|
80
|
+
|
|
81
|
+
Creates a new RevenueCat API client instance.
|
|
82
|
+
|
|
83
|
+
**Parameters:**
|
|
84
|
+
|
|
85
|
+
- `accessToken` (string, required): Your RevenueCat API key
|
|
86
|
+
- `options` (object, optional): Configuration options
|
|
87
|
+
|
|
88
|
+
**Options:**
|
|
89
|
+
|
|
90
|
+
- `automaticRateLimit` (boolean, default: true): Enable/disable automatic rate limiting
|
|
91
|
+
- `baseUrl` (string, default: `"https://api.revenuecat.com/v2"`): API base URL
|
|
92
|
+
- All other [options from `openapi-fetch`](https://openapi-ts.dev/openapi-fetch/api#createclient) are supported
|
|
93
|
+
|
|
94
|
+
**Returns:** A configured API client
|
|
95
|
+
|
|
96
|
+
## Examples
|
|
97
|
+
|
|
98
|
+
### Managing Subscriptions / Entitlements
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
// Get subscriptions information
|
|
102
|
+
const { data: subscriptions } = await client[
|
|
103
|
+
"/projects/{project_id}/customers/{customer_id}/subscriptions"
|
|
104
|
+
].GET({
|
|
105
|
+
params: {
|
|
106
|
+
path: {
|
|
107
|
+
project_id: "proj1ab2c3d4",
|
|
108
|
+
customer_id: "19b8de26-77c1-49f1-aa18-019a391603e2",
|
|
109
|
+
},
|
|
110
|
+
query: {
|
|
111
|
+
limit: 20,
|
|
112
|
+
starting_after: "ent12354",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Create entitlement
|
|
118
|
+
const { data: entitlement } = await client[
|
|
119
|
+
"/projects/{project_id}/entitlements"
|
|
120
|
+
].POST({
|
|
121
|
+
params: {
|
|
122
|
+
path: {
|
|
123
|
+
project_id: "proj1ab2c3d4",
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
body: {
|
|
127
|
+
lookup_key: "premium",
|
|
128
|
+
display_name: "Premium",
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Handling Errors
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
const { data, error } = await client[
|
|
137
|
+
"/projects/{project_id}/customers/{customer_id}"
|
|
138
|
+
].GET({
|
|
139
|
+
params: {
|
|
140
|
+
path: {
|
|
141
|
+
project_id: "proj1ab2c3d4",
|
|
142
|
+
customer_id: "19b8de26-77c1-49f1-aa18-019a391603e2",
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
if (error) {
|
|
148
|
+
switch (error.type) {
|
|
149
|
+
case "resource_missing":
|
|
150
|
+
console.log("Resource not found");
|
|
151
|
+
break;
|
|
152
|
+
case "rate_limit_error":
|
|
153
|
+
console.log("Rate limit exceeded");
|
|
154
|
+
break;
|
|
155
|
+
default:
|
|
156
|
+
console.log("Unknown error", error.message, error.doc_url);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Development
|
|
163
|
+
|
|
164
|
+
### Prerequisites
|
|
165
|
+
|
|
166
|
+
- Node.js 20+
|
|
167
|
+
- pnpm (recommended) or npm
|
|
168
|
+
|
|
169
|
+
### Setup
|
|
170
|
+
|
|
171
|
+
1. Clone the repository
|
|
172
|
+
2. Install dependencies: `pnpm install`
|
|
173
|
+
3. Generate types: `pnpm generate:openapi`
|
|
174
|
+
4. Run tests: `pnpm test`
|
|
175
|
+
|
|
176
|
+
### Scripts
|
|
177
|
+
|
|
178
|
+
- `pnpm generate:openapi` - Generate TypeScript types and client from OpenAPI spec
|
|
179
|
+
- `pnpm update:openapi` - Redownload the latest RevenueCat OpenAPI spec and then regenerate the client
|
|
180
|
+
- `pnpm test` - Run test suite
|
|
181
|
+
- `pnpm test:watch` - Run tests in watch mode
|
|
182
|
+
- `pnpm lint` - Run ESLint
|
|
183
|
+
- `pnpm types:check` - Check TypeScript types
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
MIT
|
|
188
|
+
|
|
189
|
+
## Contributing
|
|
190
|
+
|
|
191
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|