Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathrepl
More file actions
Latest commit
executable file
·103 lines (83 loc) · 3.25 KB
/
Copy pathrepl
File metadata and controls
executable file
·103 lines (83 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
'use strict';
constEventEmitter=require('events').EventEmitter;
constrepl=require('repl');
constq=require('querystring');
constpackageInfo=require('./package.json');
console.log(`Starting snoode v${packageInfo.version} repl`);
console.log('Enter `help()` for information.');
require('babel-polyfill');
constoptionsWithAuth=require('./build').optionsWithAuth;
constAPIOptions=Object.assign({},
optionsWithAuth(process.env.TOKEN),
{eventEmitter: newEventEmitter()}
);
constlocal=repl.start('$> ');
functionhelp(){
consthelp=[
'To try out the API, use the available instance of `api` to make requests.',
' For example, try writing: `SubredditEndpoint.get(APIOptions, { id: "coffee" })`',
'\n',
'The latest API error can be accessed at `error`. Requests and responses',
'can be accessed at `request` and `response`.',
'The latest API result can be accessed at `apiResponse`',
'\n',
'Also try out using Models directly, they"re all in scope',
' e.g. try typing `SubscribedSubreddits.fetch(APIOptions)` (assuming you have your token)',
' property configured',
' then type `apiResponse.results` and',
' `apiResponse.getModelFromRecord(apiResponse.results[0])`',
' if you want access to the SubredditList model that"s created in this example,',
' you"ll need to append a .then(function(res) { this.res = res; })',
' and then res will be available as a toplevel variable',
].join('\n');
console.log(help);
}
APIOptions.eventEmitter.on('response',functionprintResponse(req,res){
console.log(`${req.method}${req.url} returned ${res.statusCode}`);
local.context.request=res;
local.context.response=res;
});
APIOptions.eventEmitter.on('result',function(apiResponse){
local.context.apiResponse=apiResponse;
});
APIOptions.eventEmitter.on('error',functionprintResponse(e,req){
console.log(`${req.method}${req.url} returned error:`);
console.log(e.toString());
local.context.error=e;
});
APIOptions.eventEmitter.on('request',functionprintRequest(req){
console.log(`Initiating ${req.method} to ${req.url}`);
console.log(`query/data: ${JSON.stringify(req.query,null,2)}`);
});
local.context.findLoadMoreStub=functionfindLoadMoreStub(r){
// r is a result
r.results.forEach(topLevelRecord=>{
local.context.checkRecordForLoadMore(r,topLevelRecord);
});
};
local.context.checkRecordForLoadMore=functioncheckRecordForLoadMore(r,record){
constcomment=r.getModelFromRecord(record);
if(!comment){return;}
comment.replies.forEach(replyRecord=>{
if(replyRecord.type==='comment'){
local.context.checkRecordForLoadMore(r,replyRecord);
return;
}
console.log('load more?',replyRecord);
});
};
functionexportToContext(submoduleName){
constsubmodule=require('./build')[submoduleName];
local.context[submoduleName]=submodule;
Object.keys(submodule).forEach(function(subSubmoduleName){
local.context[subSubmoduleName]=submodule[subSubmoduleName];
});
}
local.context.APIOptions=APIOptions;
exportToContext('endpoints');
exportToContext('models');
exportToContext('collections');
exportToContext('APIResponses');
exportToContext('APIResponsePaging');
local.context.help=help;