README
wykop-v2-typescript
Simple, minimal wrapper for shitty wykop api v2
Instalation
yarn add wykop-v2-typescript
or
npm install wykop-v2-typescript
Wykop Api v2 documentation
https://www.wykop.pl/dla-programistow/apiv2/
Initialization
import wykop from "wykop-v2-typescript"
const wykop = new Wykop({
appKey: string,
appSecret: string,
secure: boolean, // if set to true url will be prefixed with 'https', otherwise 'http'
wykopUrl: string // base url for wykop (without trailing slash)
}
userkey: string // userkey uses for requests
)
Default options
{
appKey: null,
appSecret: null,
secure: true,
wykopUrl: a2.wykop.pl
}
userkey: undefined
Request parameters
wykop.request({
methods: string[] // array with strings, will be parsed as string/string/
,
namedParams: {
[key: string]: string | number
} // object with API parameters, will be parsed as key/value/key/value
,
apiParams: string[] // array with strings, will be parsed as string/string/
,
postParams: {
[key: string]: string | number
} // object will be send in body as key=value&key=value
,
reorderParams: boolean = false // If your request returns that something doesn't exists try to set this to true
})
Example request (without logging user)
- Without async/await
const wykop = new Wykop({
appKey: 'asdnasdnad',
appSecret: 'sdakdsajd',
});
wykop
.request({
methods: ['Entries', 'Hot'],
namedParams: { page: 1, period: 6 },
})
.then((res) => {
// Response from wykop
})
.catch((error) => {
// Only if something went wrong 😁
});
- With async/await
// Dont forget to put this code in async function
const wykop = new Wykop({
appKey: 'asdnasdnad',
appSecret: 'sdakdsajd',
});
try {
const response = await wykop.request({
methods: ['Entries', 'Hot'],
namedParams: { page: 1, period: 6 },
});
// Your code here
} catch (err) {
// Only if something went wrong 😁
}
Example request (with userkey)
- Without async/await
const wykop = new Wykop({
appKey: 'asdnasdnad',
appSecret: 'sdakdsajd',
});
wykop
.request({
methods: ['Login', 'Index'],
postParams: { login: 'Vegann', accountkey: 'Token from wykop connect' },
})
.then(() => {
// userkey will be stored in wykop.userkey you dont need to provide it once logged in
return wykop.request({
methods: ['Entries', 'Add'],
postParams: { body: 'Body' },
});
})
.then((res) => {
// Response from wykop
})
.catch((error) => {
// Only if something went wrong 😁
});
- With async/await
// Dont forget to put this code in async function
const wykop = new Wykop({
appKey: 'asdnasdnad',
appSecret: 'sdakdsajd',
});
try {
await wykop.request({
methods: ['Login', 'Index'],
postParams: {
login: 'Vegann',
accountkey: 'Token from wykop connect',
},
});
// userkey will be stored in wykop.userkey you dont need to provide it in next requests once logged in
const res = await wykop.request({
methods: ['Entries', 'Add'],
postParams: { body: 'Body' },
});
// Response from wykop
} catch (err) {
// Only if something went wrong 😁
}
Getting link for Wykop connect
- Without redirect
const wykop = new Wykop({
appKey: 'asdnasdnad',
appSecret: 'sdakdsajd',
});
const { url } = wykop.wykopConnectLink();
- Put
url
ina
tag
- With redirect
const wykop = new Wykop({
appKey: 'asdnasdnad',
appSecret: 'sdakdsajd',
});
const { url, secure } = wykop.wykopConnectLink('http://localhost:8080');
After user logs in to Wykop will redirect be to
http://localhost:8080/?connectData=XXXXXXXXXX
Then you can catch
XXXXXXXXXX
from url and decode user with:
JSON.parse(atob('XXXXXXXXXX'));
- Use
secure
for checking if correct user was returned