Skip to content

Commit 497ec7c

Browse files
committed
feat: initial commit
1 parent d6a6d2a commit 497ec7c

20 files changed

+919
-1
lines changed

.github/actions/redis/action.yaml

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Create local redis server
2+
3+
inputs:
4+
UPSTASH_REDIS_REST_URL:
5+
required: true
6+
UPSTASH_REDIS_REST_TOKEN:
7+
required: true
8+
UPSTASH_REPO_ACCESS_TOKEN:
9+
required: true
10+
REDIS_SERVER_CONFIG:
11+
required: true
12+
13+
runs:
14+
using: "composite"
15+
16+
steps:
17+
- name: Check out Redis Server
18+
uses: actions/checkout@v2
19+
with:
20+
repository: upstash/redis-server
21+
token: ${{ inputs.UPSTASH_REPO_ACCESS_TOKEN }}
22+
path: redis-server
23+
24+
- uses: actions/setup-go@v2
25+
with:
26+
stable: "true"
27+
go-version: "^1.17"
28+
29+
- uses: actions/cache@v2
30+
with:
31+
path: |
32+
~/go/pkg/mod
33+
~/.cache/go-build
34+
key: ${{ runner.os }}-redis-server-${{ hashFiles('**/go.mod') }}
35+
restore-keys: |
36+
${{ runner.os }}-redis-server
37+
38+
- run: |
39+
echo "$REDIS_SERVER_CONFIG" >> config.json
40+
shell: bash
41+
env:
42+
REDIS_SERVER_CONFIG: ${{ inputs.REDIS_SERVER_CONFIG }}
43+
44+
- run: |
45+
make
46+
./upstash-redis -log-level error -config $GITHUB_WORKSPACE/config.json &
47+
working-directory: ./redis-server/cmd
48+
shell: bash
49+
env:
50+
UPSTASH_REDIS_REST_URL: ${{ inputs.UPSTASH_REDIS_REST_URL }}
51+
UPSTASH_REDIS_REST_TOKEN: ${{ inputs.UPSTASH_REDIS_REST_TOKEN }}
52+
53+
- run: |
54+
curl $UPSTASH_REDIS_REST_URL/info -H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN"
55+
shell: bash
56+
env:
57+
UPSTASH_REDIS_REST_URL: ${{ inputs.UPSTASH_REDIS_REST_URL }}
58+
UPSTASH_REDIS_REST_TOKEN: ${{ inputs.UPSTASH_REDIS_REST_TOKEN }}

.github/workflows/prerelease.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Prerelease
2+
on:
3+
push:
4+
branches:
5+
- main
6+
- release
7+
jobs:
8+
prerelease:
9+
name: Prerelease
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout Repo
13+
uses: actions/checkout@v2
14+
15+
16+
- name: Setup Node
17+
uses: actions/setup-node@v2
18+
with:
19+
node-version: 16
20+
21+
- uses: denoland/setup-deno@v1
22+
with:
23+
deno-version: v1.x
24+
25+
- name: Build
26+
run: deno run -A ./cmd/build.ts
27+
28+
- name: Release
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
32+
run: npx semantic-release
33+
working-directory: dist
34+

.github/workflows/release.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
8+
jobs:
9+
release:
10+
name: Release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout Repo
14+
uses: actions/checkout@v2
15+
16+
- name: Set env
17+
run: echo "VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
18+
19+
- name: Setup Node
20+
uses: actions/setup-node@v2
21+
with:
22+
node-version: 16
23+
24+
- uses: denoland/setup-deno@v1
25+
with:
26+
deno-version: v1.x
27+
28+
- name: Build
29+
run: deno run -A ./cmd/build.ts $VERSION
30+
31+
- name: Publish
32+
if: "!github.event.release.prerelease"
33+
working-directory: ./dist
34+
run: |
35+
echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_TOKEN}}" > .npmrc
36+
npm publish --access public
37+
38+
- name: Publish release candidate
39+
if: "github.event.release.prerelease"
40+
working-directory: ./dist
41+
run: |
42+
echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_TOKEN}}" > .npmrc
43+
npm publish --access public --tag=next

.releaserc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"branches": [
3+
{
4+
"name": "release"
5+
},
6+
{
7+
"name": "main",
8+
"channel": "next",
9+
"prerelease": "next"
10+
}
11+
],
12+
"dryRun": false,
13+
"ci": true
14+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Upstash, Inc.
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.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# sdk-qstash-ts
1+
# sdk-qstash-ts

client.ts

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { HttpClient, Requester } from "./http.ts";
2+
import { Topics } from "./topic.ts";
3+
import { Endpoints } from "./endpoint.ts";
4+
export type ClientConfig = {
5+
/**
6+
* Url of the qstash api server
7+
*
8+
* @default "https://qstash.upstash.io"
9+
*/
10+
baseUrl?: string;
11+
12+
/**
13+
* The authorization token from the upstash console.
14+
*/
15+
authorization: string;
16+
};
17+
18+
type PublishRequest = {
19+
/**
20+
* The url of a publicly accessible server where you want to send this message to.
21+
* The url must have a valid scheme (http or https).
22+
*
23+
* Alternatively, you can specify a topic name or id instead of a url to publish to a topic.
24+
*/
25+
destination: string;
26+
27+
/**
28+
* The message to send.
29+
*
30+
* This can be anything, but please set the `Content-Type` header accordingly.
31+
*
32+
* You can leave this empty if you want to send a message with no body.
33+
*/
34+
body?: BodyInit;
35+
36+
/**
37+
* Optionally specify a cron expression to repeatedly send this message to the destination.
38+
*
39+
* @default undefined
40+
*/
41+
cron?: string;
42+
43+
/**
44+
* Optionally send along headers with the message.
45+
* These headers will be sent to your destination.
46+
*
47+
* We highly recommend sending a `Content-Type` header along, as this will help your destination
48+
* server to understand the content of the message.
49+
*/
50+
headers?: HeadersInit;
51+
52+
/**
53+
* Optionally delay the delivery of this message.
54+
*
55+
* In seconds.
56+
*
57+
* @default undefined
58+
*/
59+
delay?: number;
60+
61+
/**
62+
* Optionally set the absolute delay of this message.
63+
* This will override the delay option.
64+
* The message will not delivered until the specified time.
65+
*
66+
* Unix timestamp in seconds.
67+
*
68+
* @default undefined
69+
*/
70+
notBefore?: number;
71+
72+
/**
73+
* We will no longer try to deliver the message after this time
74+
*
75+
* Unix timestamp with second precicion
76+
*
77+
* @default undefined
78+
*/
79+
deadline?: number;
80+
81+
/**
82+
* Provide a unique id for deduplication. This id will be used to detect duplicate messages.
83+
* If a duplicate message is detected, the request will be accepted but not enqueued.
84+
*
85+
* We store deduplication ids for 90 days. Afterwards it is possible that the message with the
86+
* same deduplication id is delivered again.
87+
*
88+
* When scheduling a message, the deduplication happens before the schedule is created.
89+
*
90+
* @default undefined
91+
*/
92+
deduplicationID?: string;
93+
94+
/**
95+
* If true, the message content will get hashed and used as deduplication id.
96+
* If a duplicate message is detected, the request will be accepted but not enqueued.
97+
*
98+
* The content based hash includes the following values:
99+
* - All headers, except Upstash-Authorization, this includes all headers you are sending.
100+
* - The entire raw request body The destination from the url path
101+
*
102+
* We store deduplication ids for 90 days. Afterwards it is possible that the message with the
103+
* same deduplication id is delivered again.
104+
*
105+
* When scheduling a message, the deduplication happens before the schedule is created.
106+
*
107+
* @default false
108+
*/
109+
contentBasedDeduplication?: boolean;
110+
111+
/**
112+
* In case your destination server is unavaialble or returns a status code outside of the 200-299
113+
* range, we will retry the request after a certain amount of time.
114+
*
115+
* Configure how many times you would like the delivery to be retried
116+
*
117+
* @default The maximum retry quota associated with your account.
118+
*/
119+
retries?: number;
120+
};
121+
122+
export class Client {
123+
public http: Requester;
124+
125+
public constructor(config: ClientConfig) {
126+
this.http = new HttpClient({
127+
baseUrl: config.baseUrl
128+
? config.baseUrl.replace(/\/$/, "")
129+
: "https://qstash.upstash.io",
130+
authorization: config.authorization,
131+
});
132+
}
133+
134+
public get topics(): Topics {
135+
return new Topics(this.http);
136+
}
137+
public get endpoints(): Endpoints {
138+
return new Endpoints(this.http);
139+
}
140+
141+
public async publish<R extends PublishRequest = PublishRequest>(
142+
req: R,
143+
): Promise<PublishResponse<R>> {
144+
const res = await this.http.request<PublishResponse<R>>({
145+
path: ["v1", "publish", req.destination],
146+
body: req.cron,
147+
headers: req.headers,
148+
method: "POST",
149+
});
150+
return res;
151+
}
152+
}
153+
154+
type PublishResponse<PublishRequest> = PublishRequest extends { cron: string }
155+
? { scheduleID: string }
156+
: { messageID: string };

0 commit comments

Comments
 (0)