Skip to content

Commit

Permalink
feat: proxy service (#183)
Browse files Browse the repository at this point in the history
* feat: proxy service

* chore: docker publish github action

* Create spotty-bikes-dress.md

* chore: release

Signed-off-by: zaida04 <zaida04@users.noreply.github.com>

Signed-off-by: zaida04 <zaida04@users.noreply.github.com>
Co-authored-by: zaida04 <zaida04@users.noreply.github.com>
  • Loading branch information
zaida04 and zaida04 authored Oct 3, 2022
1 parent ff0d22c commit e3a50c6
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 15 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.env
43 changes: 43 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,46 @@ jobs:
run: pnpm recursive publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

docker:
runs-on: ubuntu-latest
strategy:
matrix:
service-name: [proxy]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Verify Changed files
uses: dorny/paths-filter@v2
id: verify-changed-files
with:
filters: |
service:
- 'services/${{ matrix.service-name }}/src/**'
- 'services/${{ matrix.service-name }}/package.json'
- name: Set up QEMU
if: steps.verify-changed-files.outputs.service == 'true'
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
if: steps.verify-changed-files.outputs.service == 'true'
uses: docker/setup-buildx-action@v2

- name: Login to GCR
if: steps.verify-changed-files.outputs.service == 'true'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
if: steps.verify-changed-files.outputs.service == 'true'
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}/${{ matrix.service-name }}:latest
file: services/${{ matrix.service-name }}/Dockerfile
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ coverage/
.npmrc
*.tsbuildinfo
*.log
*.env

.vscode/
.idea/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"husky": "7.0.4",
"lint-staged": "13.0.3",
"mocha": "10.0.0",
"nodemon": "^2.0.20",
"nyc": "15.1.0",
"pnpm": "7.12.2",
"prettier": "2.7.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/gil/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guildedjs/gil",
"version": "0.2.25",
"version": "0.2.26",
"description": "Framework for guilded.js that allows you to build bots with ease.",
"author": "Zaid \"Nico\" <contact@nico.engineer>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/guilded.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "guilded.js",
"version": "0.13.17",
"version": "0.13.18",
"description": "A Node.js library for the Guilded.gg (https://www.guilded.gg/) API written in TypeScript, usable in either JavaScript or TypeScript projects.",
"author": "Zaid \"Nico\" <contact@nico.engineer>",
"license": "MIT",
Expand Down
19 changes: 10 additions & 9 deletions packages/rest/lib/RestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ export class RestManager {
data: MakeOptions<B, Q>,
authenticated = true,
retryCount = 0,
): Promise<[Response, Promise<T>]> {
{ returnAsText = true, bodyIsJSON = true }: { returnAsText?: boolean; bodyIsJSON?: boolean } = {},
): Promise<[Response, Promise<T | string>]> {
const headers: HeadersInit = {};
if (authenticated) headers.Authorization = `Bearer ${this.token}`;

let body: Buffer | string | undefined = undefined;
let body: BodyInit | undefined = data.body as BodyInit;
if (data.body instanceof FormData) {
body ??= data.body.getBuffer();
Object.assign(headers, { ...data.body.getHeaders() });
} else {
} else if (bodyIsJSON) {
body ??= JSON.stringify(body);
}

Expand Down Expand Up @@ -90,7 +91,7 @@ export class RestManager {
throw new GuildedAPIError(parsedResponse.message, data.method, data.path, response.status);
}

return [response, response.json().catch(() => ({})) as Promise<T>];
return [response, returnAsText ? response.text() : (response.json().catch(() => ({})) as Promise<T>)];
}

public get<T extends JSONB, Q = RequestBodyObject>(path: string, query?: Q, authenticated = true): Promise<T> {
Expand All @@ -101,7 +102,7 @@ export class RestManager {
query,
},
authenticated,
).then((x) => x[1]);
).then((x) => x[1] as Promise<T>);
}

public post<T extends JSONB, B = RequestBodyObject>(path: string, body?: B, authenticated = true): Promise<T> {
Expand All @@ -112,7 +113,7 @@ export class RestManager {
path,
},
authenticated,
).then((x) => x[1]);
).then((x) => x[1] as Promise<T>);
}

public delete<T extends JSONB, B = RequestBodyObject>(path: string, body?: B, authenticated = true): Promise<T> {
Expand All @@ -123,7 +124,7 @@ export class RestManager {
path,
},
authenticated,
).then((x) => x[1]);
).then((x) => x[1] as Promise<T>);
}

public patch<T extends JSONB, B = RequestBodyObject>(path: string, body: B, authenticated = true): Promise<T> {
Expand All @@ -134,7 +135,7 @@ export class RestManager {
path,
},
authenticated,
).then((x) => x[1]);
).then((x) => x[1] as Promise<T>);
}

public put<T extends JSONB, B = RequestBodyObject>(path: string, body?: B, authenticated = true): Promise<T> {
Expand All @@ -145,7 +146,7 @@ export class RestManager {
path,
},
authenticated,
).then((x) => x[1]);
).then((x) => x[1] as Promise<T>);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guildedjs/rest",
"version": "2.8.8",
"version": "2.9.0",
"description": "Rest structure for guilded.js",
"author": "Zaid \"Nico\" <contact@nico.engineer>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/webhook-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guildedjs/webhook-client",
"version": "0.4.0",
"version": "0.4.1",
"description": "Structures and clients for webhooks in Guilded.",
"author": "Zaid \"Nico\" <contact@nico.engineer>",
"license": "MIT",
Expand Down
87 changes: 86 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages:
- "packages/**"
- "services/**"
26 changes: 26 additions & 0 deletions services/proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM node:16-alpine
LABEL name "guildedjs rest proxy builder"
WORKDIR /opt/build

RUN apk add --update \
&& apk add --no-cache --virtual .build-deps curl \
&& curl -L https://unpkg.com/@pnpm/self-installer | node

COPY tsconfig.json package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY ./packages/rest/. ./packages/rest/
COPY ./packages/guilded-api-typings/. ./packages/guilded-api-typings/.

COPY ./services/proxy/package.json ./services/proxy/tsconfig.json ./services/proxy/
RUN pnpm install -r --frozen-lockfile

COPY ./services/proxy/src/. ./services/proxy/src/
RUN pnpm run build && rm -rf node_modules/ && pnpm install --prod --ignore-scripts

FROM node:16-alpine
LABEL name "guildedjs rest proxy"
WORKDIR /usr/app

COPY --from=0 ./opt/build ./

WORKDIR /usr/app/services/proxy
CMD ["node", "dist/index.js"]
6 changes: 6 additions & 0 deletions services/proxy/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"watch": ["src"],
"ext": "ts,json",
"exec": "npx tsc && node dist/index.js",
"legacyWatch": true
}
Loading

0 comments on commit e3a50c6

Please sign in to comment.