Skip to content

Commit

Permalink
build: minor fixes & doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-fish committed Jul 10, 2023
1 parent 91d0bf0 commit 1a1a4a8
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 11 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ const api = new MediaWikiApi('https://zh.moegirl.org.cn/api.php')

**在浏览器中直接使用/Use directly in the browser**

```ts
import('https://unpkg.com/wiki-saikou?module').then(({ MediaWikiApi }) => {
```js
import('https://unpkg.com/wiki-saikou').then(() => {
const { MediaWikiApi } = globalThis.WikiSaikou
const api = new MediaWikiApi('https://zh.moegirl.org.cn/api.php')
// ...
})
Expand All @@ -71,9 +72,9 @@ Below is the documentation of MediaWikiApi.
- **Not required but with conditions**: If you are using it in the browser environment, and the website runs MediaWiki. The instance will automatically use the API endpoint of current wiki.
- `options`: {AxiosRequestConfig}

#### `login(username: string, password: string): Promise<{ status: 'PASS' | 'FAIL'; username: string }>`
#### `login(username: string, password: string): Promise<{ result: 'Success' | 'Failed'; lguserid: number; lgusername: string }>`

Login you account.
Login your account.

#### `get<T = any>(params: MwApiParams, options?: AxiosRequestConfig): Promise<AxiosResponse<T>>`

Expand Down
13 changes: 13 additions & 0 deletions debug/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { MediaWikiApi } = require('..')
const { env } = require('node:process')

const api = new MediaWikiApi('https://zh.moegirl.org.cn/api.php', {
headers: {
'api-user-agent': env.MOEGIRL_API_USER_AGENT || '',
},
})

const username = env.MOEGIRL_USERNAME || ''
const password = env.MOEGIRL_PASSWORD || ''

api.login(username, password).then(console.info)
13 changes: 13 additions & 0 deletions debug/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaWikiApi } from '../dist/index.js'
import { env } from 'node:process'

const api = new MediaWikiApi('https://zh.moegirl.org.cn/api.php', {
headers: {
'api-user-agent': env.MOEGIRL_API_USER_AGENT || '',
},
})

const username = env.MOEGIRL_USERNAME || ''
const password = env.MOEGIRL_PASSWORD || ''

api.login(username, password).then(console.info)
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"browser": "./dist/index.umd.js",
"exports": "./dist/index.mjs",
"files": [
"dist",
"lib"
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

File renamed without changes.
File renamed without changes.
File renamed without changes.
57 changes: 57 additions & 0 deletions test/4.actions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'
import { env } from 'process'
import { MediaWikiApi } from '../src/index'

const api = new MediaWikiApi('https://zh.moegirl.org.cn/api.php', {
headers: {
'api-user-agent': env.MOEGIRL_API_USER_AGENT || '',
},
})

const username = env.MOEGIRL_USERNAME || ''
const password = env.MOEGIRL_PASSWORD || ''

const now = new Date()
const editTitle = `User:${username}/sandbox/wiki-saikou`
let editPageid = 0
let editNewrevid = 0

describe('Actions', () => {
it('Login', async () => {
const login = await api.login(username, password)
expect(login.result).to.equal('Success')
})

it('Do edit', async () => {
const {
data: { edit },
} = await api.postWithEditToken({
action: 'edit',
title: editTitle,
text: now.toISOString(),
summary: '[Automatic] Unit tests for https://npm.im/wiki-saikou',
})
expect(edit.result).to.equal('Success')
editPageid = edit.pageid
editNewrevid = edit.newrevid
})

it('Check edit contents', async () => {
const {
data: {
query: {
pages: [page],
},
},
} = await api.get({
action: 'query',
revids: editNewrevid,
prop: 'revisions',
rvprop: 'user|content',
})
expect(page.pageid).to.equal(editPageid)
expect(page.revisions[0].user).to.equal(username)
expect(page.revisions[0].content).to.equal(now.toISOString())
})
})
1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
dts: true,
outDir: 'dist',
target: ['es2018'],
globalName: 'WikiSaikou',
format: ['esm', 'cjs', 'iife'],
legacyOutput: true,
splitting: true,
Expand Down
4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import dts from 'vite-plugin-dts'
export default defineConfig({
build: {
lib: {
name: 'MediaWikiApi',
name: 'WikiSaikou',
fileName: 'index',
entry: resolve(__dirname, 'src/index.ts'),
formats: ['cjs', 'es', 'iife', 'umd'],
formats: ['umd'],
},
sourcemap: true,
},
Expand Down

0 comments on commit 1a1a4a8

Please sign in to comment.