Skip to content

Commit

Permalink
add api sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Mineev committed Aug 20, 2022
1 parent 75427df commit 5f376b1
Show file tree
Hide file tree
Showing 33 changed files with 2,797 additions and 131 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
- "docker-init.sh"
- "Dockerfile"
- "!**.md"
- "!frontend/packages/**"
pull_request:
paths:
- ".github/workflows/ci-build.yml"
Expand All @@ -22,6 +23,7 @@ on:
- "docker-init.sh"
- "Dockerfile"
- "!**.md"
- "!frontend/packages/**"

jobs:
build-images:
Expand Down
153 changes: 153 additions & 0 deletions .github/workflows/ci-frontend-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
name: "@remark42/api"

on:
push:
branches:
- master
paths:
- ".github/workflows/ci-frontend-api.yml"
- "frontend/packages/**"
- "!**.md"
pull_request:
paths:
- ".github/workflows/ci-frontend-api.yml"
- "frontend/packages/**"
- "!**.md"

jobs:
type-check:
name: Type check
runs-on: ubuntu-latest
strategy:
matrix:
node: [16.15.1]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install pnpm
uses: pnpm/[email protected]
id: pnpm-install
with:
version: 7
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm i
working-directory: ./frontend

- name: Run type check
run: pnpm type-check:api
working-directory: ./frontend

lint:
name: Lint
runs-on: ubuntu-latest
strategy:
matrix:
node: [16.15.1]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install pnpm
uses: pnpm/[email protected]
id: pnpm-install
with:
version: 7
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm i
working-directory: ./frontend

- name: Run linters
run: pnpm lint:api
working-directory: ./frontend/

test:
name: Tests & Coverage
runs-on: ubuntu-latest
strategy:
matrix:
node: [16.15.1]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install pnpm
uses: pnpm/[email protected]
id: pnpm-install
with:
version: 7
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm i
working-directory: ./frontend

- name: Test & Coverage
run: pnpm coverage:api
working-directory: ./frontend

- name: Submit coverage
run: ${{ github.workspace }}/frontend/apps/remark42/node_modules/.bin/codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
9 changes: 7 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
],
"scripts": {
"prepare": "if [ -z \"$CI\" ]; then cd .. && husky install frontend/.husky; else echo \"Skip Husky Hooks\"; fi",
"lint-staged": "lint-staged"
"lint-staged": "lint-staged",
"test:api": "turbo run test --filter=@remark42/api",
"coverage:api": "turbo run coverage --filter=@remark42/api",
"type-check:api": "turbo run type-check --filter=@remark42/api",
"lint:api": "turbo run lint --filter=@remark42/api"
},
"devDependencies": {
"husky": "^8.0.1",
"lint-staged": "^13.0.3"
"lint-staged": "^13.0.3",
"turbo": "^1.4.3"
}
}
2 changes: 2 additions & 0 deletions frontend/packages/api/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
coverage
22 changes: 22 additions & 0 deletions frontend/packages/api/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:@typescript-eslint/strict',
'plugin:prettier/recommended',
],
overrides: [],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: './tsconfig.eslint.json',
},
plugins: ['@typescript-eslint'],
}
5 changes: 5 additions & 0 deletions frontend/packages/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
coverage
dist
**/*.js
**/*.d.ts
3 changes: 3 additions & 0 deletions frontend/packages/api/.lintstagedrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'./**/*.ts': ['pnpm lint-staged:lint'],
}
17 changes: 17 additions & 0 deletions frontend/packages/api/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
coverage/**
dist/**
node_modules/**
src/**
test/**
.editorconfig
.eslintrc.cjs
.prettierrc
files.txt
jest.config.ts
pnpm-lock.yaml
postpublish.sh
prepublish.sh
tsconfig.common.json
tsconfig.dev.json
tsconfig.json

1 change: 1 addition & 0 deletions frontend/packages/api/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.17.0
6 changes: 6 additions & 0 deletions frontend/packages/api/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"printWidth": 100,
"quoteProps": "consistent",
"singleQuote": true
}
11 changes: 11 additions & 0 deletions frontend/packages/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# @remark42/api

Implementation of API methods for Remark42

## Development

- If you don't have `pnpm` installed run `npm i -g pnpm`
- Install dependencies `pnpm i`
- Run development mode with `pnpm run dev`
- Build lib with `pnpm run build`
- Run tests with `pnpm run test`
134 changes: 134 additions & 0 deletions frontend/packages/api/clients/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import type { ClientParams, User } from './index'
import { API_BASE } from '../consts'
import { createFetcher } from '../lib/fetcher'

export type BlockTTL = 'permanently' | '43200m' | '10080m' | '1440m'
export interface BlockUserResponse {
block: boolean
site_id: string
user_id: string
}

export function createAdminClient({ siteId, baseUrl }: ClientParams) {
const fetcher = createFetcher(siteId, `${baseUrl}${API_BASE}`)

async function toggleUserVerification(id: string, verified: 0 | 1): Promise<void> {
return fetcher.put(`/verify/${id}`, { verified })
}

async function toggleCommentPin(id: string, pinned: 0 | 1): Promise<void> {
return fetcher.put(`/pin/${id}`, { pinned })
}

async function toggleCommenting(url: string, ro: 0 | 1): Promise<void> {
return fetcher.put('/readonly', { url, ro })
}

async function toggleUserBlock(id: string, ttl?: BlockTTL): Promise<BlockUserResponse> {
const params = ttl
? {
block: 1,
ttl: ttl === 'permanently' ? 0 : ttl,
}
: { block: 0 }

return fetcher.put<BlockUserResponse>(`/user/${id}`, params)
}

/**
* Request list of blocked users
*/
async function getBlockedUsers(): Promise<User[]> {
return fetcher.get<User[]>('/blocked')
}

/**
* Block user from commenting
* @param id user ID
* @param ttl block duration
*/
async function blockUser(id: string, ttl: BlockTTL): Promise<BlockUserResponse> {
return toggleUserBlock(id, ttl)
}

/**
* Unblock user from commenting
* @param id user ID
*/
async function unblockUser(id: string): Promise<BlockUserResponse> {
return toggleUserBlock(id)
}

/**
* Mark user as verified
* @param id user ID
*/
async function verifyUser(id: string): Promise<void> {
return toggleUserVerification(id, 1)
}
/**
* Mark user as unverified
* @param id user ID
*/
async function unverifyUser(id: string): Promise<void> {
return toggleUserVerification(id, 0)
}
/**
* Approve request to remove user data
* @param token token from email
*/
async function approveRemovingRequest(token: string): Promise<void> {
return fetcher.get('/deleteme', { token })
}

/**
* Mark comment as pinned
* @param id comment ID
*/
async function pinComment(id: string): Promise<void> {
return toggleCommentPin(id, 1)
}
/**
* Mark comment as unpinned
* @param id comment ID
*/
async function unpinComment(id: string): Promise<void> {
return toggleCommentPin(id, 0)
}
/**
* Remove comment
* @param url page URL
* @param id comment ID
*/
async function removeComment(url: string, id: string): Promise<void> {
return fetcher.delete(`/comment/${id}`, { url })
}
/**
* Enable commenting on a page
* @param url page URL
*/
async function enableCommenting(url: string) {
return toggleCommenting(url, 1)
}
/**
* Disable commenting on a page
* @param url page URL
*/
async function disableCommenting(url: string) {
return toggleCommenting(url, 0)
}

return {
getBlockedUsers,
blockUser,
unblockUser,
verifyUser,
unverifyUser,
approveRemovingRequest,
pinComment,
unpinComment,
removeComment,
enableCommenting,
disableCommenting,
}
}
Loading

0 comments on commit 5f376b1

Please sign in to comment.