Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit f941fe8

Browse files
committed
Added a few examples on the setup and usage
1 parent 55dd7b5 commit f941fe8

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
local
1+
local
2+
examples

examples/get-name.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Schema = require('tf2-schema');
2+
3+
const schemaManager = new Schema({ apiKey: 'your steam api key' });
4+
5+
schemaManager.init(function (err) {
6+
if (err) {
7+
throw err;
8+
}
9+
});
10+
11+
schemaManager.on('ready', function () {
12+
// item object to get the name off, only the defindex and quality is required
13+
const item = {
14+
defindex: 5021,
15+
quality: 6
16+
};
17+
18+
// get the name of the item
19+
const name = schemaManager.schema.getName(item);
20+
21+
console.log(name); // -> Mann Co. Supply Crate Key
22+
});

examples/schema-cache.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Schema = require('tf2-schema');
2+
const fs = require('fs');
3+
4+
const SCHEMA_PATH = './path-to-schema-file.json';
5+
6+
const schemaManager = new Schema({ apiKey: 'your steam api key' });
7+
8+
if (fs.existsSync(SCHEMA_PATH)) {
9+
// a cached schema exists
10+
11+
// read and parse the cached schema
12+
const cachedData = JSON.parse(fs.readFileSync(SCHEMA_PATH));
13+
14+
// set the schema data
15+
schemaManager.setSchema(cachedData);
16+
}
17+
18+
// initialize tf2-schema like normally
19+
schemaManager.init(function (err) {
20+
if (err) {
21+
throw err;
22+
}
23+
});
24+
25+
schemaManager.on('schema', function (schema) {
26+
// this event is emitted when the schema has been fetched
27+
28+
// writes the schema data to disk
29+
fs.writeFileSync(SCHEMA_PATH, JSON.stringify(schemaManager.toJSON()));
30+
});

examples/setup.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Schema = require('tf2-schema');
2+
3+
// Create new instance of tf2-schema
4+
const schema = new Schema({ apiKey: 'your steam api key' });
5+
6+
// Initialize tf2-schema
7+
schema.init(function (err) {
8+
if (err) {
9+
// Error occurred fetching the schema
10+
throw err;
11+
}
12+
13+
// tf2-schema is now ready to be used
14+
});
15+
16+
schema.on('ready', function () {
17+
// this event is emitted when the init function has been called and finished without errors
18+
});

0 commit comments

Comments
 (0)