心知天气 API Node.js(Support Typescript)SDK 【语言--中文】
$ npm i seniverse-api --save
import { SeniverseV3 } from 'seniverse-api'
const seniverseV3 = new SeniverseV3({
encryption: {
uid: '', // Public Key
key: '', // Private Key
ttl: 10000, // Signature expiration times
enabled: false // Enable Signature verification or not
},
query: {
unit: 'c', // Unit
language: '', // Return language
timeouts: [3000, 3000] // [retry times,overtime times]
},
// memory cache
cache: {
ttl: 100, // Cache time,unit is second,It can set as 'auto'
max: 1000, // Numbers of cache data
enabled: true // Enable open cache or not
},
returnRaw: false // Return directly the API's raw data or not
})
await seniverseV3.weather.daily.data({ days: 2, start: -1, location: 'beijing' })
await seniverseV3.air.hourlyHistory.data({ scope: 'city', location: 'beijing' })
// Called via API URL
await seniverseV3.request(
'/weather/daily',
{ days: 2, start: -1, location: 'beijing' }
)
// Generate jsonp call the link
seniverseV3.jsonp(
'/weather/daily',
{
encryption: {
ttl: 1000,
uid: '',
key: '',
},
query: {
callback: 'weatherDaily',
location: 'beijing'
}
}
)
import { SeniverseV3 } from 'seniverse-api'
const seniverseV3 = new SeniverseV3({
// Memory cache
cache: {
ttl: 100, // Cache time, unit is second
max: 1000, // Numbers of cache data
enabled: true // Enable open cache or not
},
encryption: {
uid: '', // public key
key: '', // private key
ttl: 100, // Signature expiration times, unit is second
enabled: true // Enable Signature verification or not
},
query: {
language: 'zh-Hans', // Return language, can change in calling
location: 'beijing', // Position, can change in calling
unit: 'c', // Unit, can change in calling
timeouts: [3000, 5000, 7000] // Numbers of retry and overtime times, unit is millisecond
},
returnRaw: false // Return directly the API's raw data or not
})
配置说明:
encryption
: API Encryption/verification configurationuid
: string, public key, doc: https://docs.seniverse.com/api/start/key.htmlkey
: string, Private key, doc: https://docs.seniverse.com/api/start/key.htmlttl
: number, Encryption expiration times, unit is second, doc:https://docs.seniverse.com/api/start/validation.htmlenabled
: boolean, Encryption or not,default istrue
cache
: Memory cache for request resultttl
: number | string, Cache times,unit is second;or set asauto
, Different cache times will be set according to different APIs(According to API update frequency)max
: number, Data buffer. Overflow will overwrite old cache.enabled
: boolean, Open cache or not. If the timeliness of the data is very high, it is not recommended to enable the cache. default isfalse
query
: Request parametertimeouts
: number[], Numbers of retry and overtime times, unit is millisecond. default is[3000, 5000, 7000]
language
: string, Return language, can changed by passed parameters in calling.doc:https://docs.seniverse.com/api/start/language.html default iszh-Hans
location
: string, Request position, can changed by passed parameters in calling.doc:https://docs.seniverse.com/api/start/common.htmlunit
: string, Request unit,can changed by passed parameters in calling.doc:https://docs.seniverse.com/api/start/common.html default isc
returnRaw
: boolean, Return directly the API's raw data or not. default isfalse
First know the 心知天气API's URL,doc:https://docs.seniverse.com/
When getting API data through the SDK, you need to call it based on the specific URL of the API, for example:
import { SeniverseV3 } from 'seniverse-api'
const seniverseV3 = new SeniverseV3({ /* your config */ })
// to real wheather API: https://api.seniverse.com/v3/weather/now.json
await seniverseV3.weather.now.data({
location: 'beijing',
language: 'zh-Hans',
unit: 'c'
})
// to weather for past 24 hours API:https://api.seniverse.com/v3/air/hourly_history.json
await seniverseV3.air.hourlyHistory.data({
location: 'beijing',
language: 'zh-Hans',
scope: 'city'
})
// For the lunar calendar API:https://api.seniverse.com/v3/life/chinese_calendar.json
await seniverseV3.life.chineseCalendar.data({
days: 2,
start: 0
})
That is, the API call rule is that the way the chain is called will be determined by the API URL.
If the URL contains an underscore _
, it should be converted to a camel style:
/v3/weather/now.json => weather.now
/v3/air/hourly_history.json => air.hourlyHistory
/v3/life/chinese_calendar.json => life.chineseCalendar
Data calls are made through the route of the known weather API, which is more in line with the old user's usage habits.
import { SeniverseV3 } from 'seniverse-api'
const seniverseV3 = new SeniverseV3({ /* your config */ })
// 调用 https://api.seniverse.com/v3/weather/daily.json
await seniverseV3.request(
'/weather/daily',
{ days: 2, start: -1, location: 'beijing' }
)
// 调用 https://api.seniverse.com/v3/air/hourly_history.json
await seniverseV3.request(
'/air/hourly_history',
{ scope: 'city', location: 'beijing' }
)
Use JSONP method call:description doc
import { SeniverseV3 } from 'seniverse-api'
const seniverseV3 = new SeniverseV3({ /* your config */ })
// Generate JSONP link call https://api.seniverse.com/v3/weather/daily.json
const url = seniverseV3.jsonp(
'/weather/daily',
{
encryption: {
ttl: 1000, // Encryption expiration time, if configured during initialization, you can leave it blank
uid: '', // The public key, if configured during initialization, can be leave blank
key: '', // The private key, if configured during initialization, can be leave blank
},
query: {
callback: 'weatherDaily', // Callback function name
location: 'beijing' // Request parameter
}
}
)
seniverseV3.jsonp(
'/air/hourly',
{
// If ttl, uid, key are passed at initialization, the encryption field can be leave blank
query: {
callback: 'airHourly', // Callback function name
location: 'beijing' // Request parameter
}
}
)
Determine whether to return the original data by setting returnRaw
when initializing SeniverseV3
.(that is the same data as in the API document)
- If
returnRaw: false
, all interfaces return data as an array. Compare with Raw API doc, The returned result has been extracted from theresults
field, and the specific data is encapsulated into thedata
(array) field, eliminating the problem that the original API returns the result format is not uniform. - If
returnRaw: true
, the returned result will not be processed, consistent with the results shown in the API documentation.